5.8. Models Field Special¶
models.UUIDField
- a field for storing UUID values (universally unique identifiers)models.CommaSeparatedIntegerField
- aCharField
that checks that the value is a comma-separated list of integersmodels.GenericIPAddressField
- aCharField
that checks that the value is a valid IPv4 or IPv6 addressmodels.IPAddressField
- deprecated in favor ofGenericIPAddressField
models.JSONField
- stores a field for storing JSON-encoded data. In Python, it is a dictmodels.GeneratedField
- a field that is automatically populated with a value when the model is saved
>>>
>>> from uuid import uuid4
... from django.db import models
... from django.utils.translation import gettext_lazy as _
...
...
... class Customer(models.Model):
... uuid = models.UUIDField(verbose_name=_('Unique UUID'), unique=True, null=False, blank=False, default=uuid4, editable=False)
... ip_address = models.GenericIPAddressField(verbose_name=_('IP Address'), null=True, blank=True, default=None, editable=False)
... payload = models.JSONField(verbose_name=_('Payload'), null=True, blank=True, default=None)
...
... def __str__(self):
... return f'{self.firstname} {self.lastname}'
...
... class Meta:
... verbose_name = _('Customer')
... verbose_name_plural = _('Customers')
5.8.1. GeneratedField¶
https://docs.djangoproject.com/en/stable/ref/models/fields/#django.db.models.GeneratedField
Example: slug, age, area, etc.
db_persist: If
True
, the field value will be saved to the databasedb_persist: If
False
, the field will be calculated on the fly (like@property
)
A field that is always computed based on other fields in the model. This
field is managed and updated by the database itself. Uses the
GENERATED ALWAYS
SQL syntax.
There are two kinds of generated columns: stored and virtual. A stored generated column is computed when it is written (inserted or updated) and occupies storage as if it were a regular column. A virtual generated column occupies no storage and is computed when it is read. Thus, a virtual generated column is similar to a view and a stored generated column is similar to a materialized view.
The expressions should be deterministic and only reference fields within the model (in the same database table). Generated fields cannot reference other generated fields. Database backends can impose further restrictions.
PostgreSQL only supports persisted columns. Oracle only supports virtual columns.
Since the database always computed the value, the object must be reloaded to
access the new value after save()
, for example, by using
refresh_from_db()
.
>>>
... from django.db import models
... from django.db.models import F
...
...
... class Square(models.Model):
... side = models.IntegerField()
... area = models.GeneratedField(
... expression=F("side") * F("side"),
... output_field=models.BigIntegerField(),
... db_persist=True,
... )
5.8.2. Arguments¶
blank
choices
db_column
db_index
default
editable
error_message
help_text
limit_choices_to
max_length
null
primary_key
unique
validators
verbose_name