Skip to content

Number models

django_rubble.models.number_models.NumberedModel

Bases: Model

Adds a number field that uses DocumentNumber to generate values.

Adds a natural_key method to the model, but no manager that uses it.

Example:

class Invoice(NumberedModel):
    number_config = SerialNumberConfig(
            prefix="INV",
            width=4,
            initial_value=1,
            step=1
        )

Attributes:

Name Type Description
number_config SerialNumberConfig

A SerialNumberConfig instance.

Source code in django_rubble\models\number_models.py
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
class NumberedModel(models.Model):
    """Adds a `number` field that uses `DocumentNumber` to generate values.

    Adds a `natural_key` method to the model, but no manager that uses it.

    Example:

        class Invoice(NumberedModel):
            number_config = SerialNumberConfig(
                    prefix="INV",
                    width=4,
                    initial_value=1,
                    step=1
                )

    Attributes:
        number_config: A SerialNumberConfig instance.
    """

    number = models.CharField(_("number"), unique=True, max_length=10, editable=False)
    number_config: SerialNumberConfig

    class Meta:
        abstract = True

    def save(self, *args, **kwargs) -> None:
        if self._state.adding:
            self.number, _ = NamedSerialNumber.objects.get_serial_number(self)
            msg = f"New number is: {self.number}"
            logger.debug(msg)
        return super().save(*args, **kwargs)

    def natural_key(self):
        return (self.number,)

    @classmethod
    def check(cls, **kwargs):
        errors = super().check(**kwargs)
        errors.extend(cls._check_number_config(**kwargs))
        return errors

    @classmethod
    def _check_number_config(cls, **kwargs):
        if not hasattr(cls, "number_config"):
            return must_be(
                "a SerialNumberConfig instance",
                option="number_config",
                obj=cls,
                error_id="rubble.M003",
            )
        if not isinstance(cls.number_config, SerialNumberConfig):
            return must_be(
                "a SerialNumberConfig instance",
                option="number_config",
                obj=cls,
                error_id="rubble.M003",
            )
        return []

django_rubble.models.number_models.SerialNumberConfig

Bases: BaseModel

Configuration for NumberModel numbering.

Example

SerialNumberConfig(prefix="INV", width=4, initial_value=1, step=1) -> INV0001, INV0002, etc.

Attributes:

Name Type Description
name str | None

The name of the model, used for NamedSerialNumber.

initial_value int

The starting value for the serial number.

step int

The increment value for the serial number.

prefix str

The prefix for the serial number.

Source code in django_rubble\models\number_models.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class SerialNumberConfig(BaseModel):
    """Configuration for NumberModel numbering.

    Example:
        `SerialNumberConfig(prefix="INV", width=4, initial_value=1, step=1)` ->
        `INV0001`, `INV0002`, etc.

    Attributes:
        name: The name of the model, used for NamedSerialNumber.
        initial_value: The starting value for the serial number.
        step: The increment value for the serial number.
        prefix: The prefix for the serial number."""

    name: str | None = None
    initial_value: int = 1
    step: int = 1
    prefix: str
    width: int