Sequence Generation Quick Start
Developer guide for the Enezaframework sequence generation. Part of the Sequence Generation series.
Table of Contents
- Overview
- Step 1 — Define the Model
- Step 2 — Define Project Choices (Optional)
- Step 3 — Configure Settings
- Step 4 — Create the Sequence Configuration
- Result
- Related Guides
Overview
A sequenced model needs three things: a base model that includes SequenceMixin (EnezaMaster or EnezaDocument), a nullable field to hold the number, and two attributes — sequence_field_name and document_name. The number format itself comes from a SequenceConfig record.
flowchart LR
A["1. Model<br/>EnezaMaster + attributes"] --> B["2. Project choices<br/>optional"]
B --> C["3. settings.py<br/>choice paths"]
C --> D["4. SequenceConfig row<br/>prefix/delimiter/length"]
D --> E["5. First save -> CUST-0001"]Step 1 — Define the Model
# myapp/models/customer.py
from django.db import models
from enezaframework.models import EnezaMaster
from myapp.choices import ProjectDocumentTypes
class Customer(EnezaMaster):
customer_code = models.CharField(
max_length=50,
null=True,
blank=True,
verbose_name="Customer Code",
)
name = models.CharField(
max_length=200,
verbose_name="Name",
)
email = models.EmailField(
verbose_name="Email",
)
sequence_field_name = 'customer_code'
document_name = ProjectDocumentTypes.CUSTOMER
class Meta:
verbose_name = "Customer"
verbose_name_plural = "Customers"
def __str__(self):
return self.customer_code or self.name
Requirements:
- The sequence field must be nullable (
null=True, blank=True) because it is filled on save. sequence_field_namenames that field.document_namemust equal a value in the active document choices.
Step 2 — Define Project Choices (Optional)
# myapp/choices.py
from django.db import models
class ProjectDocumentTypes(models.TextChoices):
CUSTOMER = 'CUST', "Customer"
VENDOR = 'VEND', "Vendor"
INVOICE = 'INV', "Invoice"
PURCHASE_ORDER = 'PO', "Purchase Order"
If you skip this step, the framework's default NumberSequencesDocument is used. See Sequence Document & Module Choices.
Step 3 — Configure Settings
# settings.py
MY_PROJECT_NUMBER_SEQUENCE_DOCUMENT = 'myapp.choices.ProjectDocumentTypes'
MY_PROJECT_MODULES = 'myapp.choices.ProjectModules'
The setting names are
MY_PROJECT_NUMBER_SEQUENCE_DOCUMENTandMY_PROJECT_MODULES. Both are optional; when omitted, the framework defaults apply.
Step 4 — Create the Sequence Configuration
In the admin, open System Administrator → Number Sequences and create an active configuration:
| Field | Value |
|---|---|
| Document | Customer |
| Prefix | CUST |
| Delimiter | - |
| Number Length | 4 |
| Active | Yes |
| Module | CRM |
See Sequence Admin Setup.
Result
The first Customer save gets CUST-0001, the next CUST-0002, and so on. No value is supplied by the caller — the mixin fills the field when it is empty on a new record.
sequenceDiagram
actor U as User
participant C as Customer.save()
participant M as SequenceMixin
participant S as SequenceConfig
U->>C: create customer (customer_code empty)
C->>M: save()
M->>S: filter(documents='CUST', active=True).first()
S-->>M: config(nextnumber=1, number_length=4)
M->>S: nextnumber = 2; save()
M->>C: customer_code = 'CUST-0001'
C-->>U: saved with CUST-0001