Skip to content

Models & fields

dorm.Model

dorm.models.Model

Base class for all ORM models.

save(using: str = 'default', force_insert: bool = False, force_update: bool = False, update_fields: list[str] | None = None) -> None

asave(using: str = 'default', force_insert: bool = False, force_update: bool = False, update_fields: list[str] | None = None) -> None async

delete(using: str = 'default') -> tuple[int, dict[str, int]]

adelete(using: str = 'default') -> tuple[int, dict[str, int]] async

refresh_from_db(using: str = 'default', fields=None)

Re-fetch this row from the database, optionally restricting to a subset of columns.

When fields= is given, the SELECT is narrowed to those columns via :meth:QuerySet.only so the database transfers only what's actually being refreshed — important on tables with large TEXT/BLOB/JSON columns where the previous SELECT * paid a real bandwidth cost. Unknown field names are silently skipped (matches the long-standing behaviour callers depend on for partial refreshes after a __set_changed hook).

arefresh_from_db(using: str = 'default', fields=None) async

Async counterpart of :meth:refresh_from_db. Same fields= narrowing applies.

full_clean(exclude: list[str] | None = None) -> None

Field types

dorm.fields.Field

Bases: Generic[_T]

Base class for all dorm fields.

The generic parameter _T is the Python type the field stores. Static type checkers use it via the overloaded __get__ so user.name (where name = CharField(...)) is inferred as str, not Any. Runtime behaviour is identical regardless of the parameter.

deconstruct() -> tuple[str | None, str, list, dict]

Return a 4-tuple suitable for migration serialisation: (name, dotted_class_path, args, kwargs).

Mirrors Django's :meth:django.db.models.Field.deconstruct so custom fields the user writes — and migration tools that consume them — can reconstruct a Field instance from the serialised form by:

.. code-block:: python

from importlib import import_module
mod_path, _, cls_name = path.rpartition(".")
cls = getattr(import_module(mod_path), cls_name)
field = cls(*args, **kwargs)

The default implementation walks the constructor's keyword arguments and emits whichever ones differ from the framework-shipped defaults. Subclasses with extra constructor parameters should override and call super() first to extend the kwargs dict.

dorm.fields.CharField

Bases: Field[str]

dorm.fields.TextField

Bases: Field[str]

dorm.fields.IntegerField

Bases: Field[int]

dorm.fields.BigIntegerField

Bases: IntegerField

dorm.fields.SmallIntegerField

Bases: IntegerField

dorm.fields.PositiveIntegerField

Bases: IntegerField

dorm.fields.PositiveSmallIntegerField

dorm.fields.FloatField

Bases: Field[float]

dorm.fields.DecimalField

Bases: Field[Decimal]

dorm.fields.BooleanField

Bases: Field[bool]

dorm.fields.DateField

Bases: Field[date]

dorm.fields.DateTimeField

Bases: Field[datetime]

Datetime column.

settings.USE_TZ controls timezone handling:

  • False (default, Django <4 behaviour): naive datetimes are stored as-is. Aware datetimes are stored verbatim.
  • True (Django ≥4 behaviour): every read returns a tz-aware datetime. Naive datetimes coming in from the user are interpreted in settings.TIME_ZONE and converted to UTC before storage. PostgreSQL columns become TIMESTAMP WITH TIME ZONE so the engine round-trips the offset.

dorm.fields.TimeField

Bases: Field[time]

dorm.fields.UUIDField

Bases: Field[UUID]

dorm.fields.EmailField

Bases: CharField

dorm.fields.URLField

Bases: CharField

dorm.fields.SlugField

Bases: CharField

dorm.fields.JSONField

Bases: Field[Any]

dorm.fields.BinaryField

Bases: Field[bytes]

dorm.fields.ArrayField

Bases: Field[list]

PostgreSQL native array column.

Stores a homogeneous list of values whose element type is given by base_field. SQLite doesn't have arrays — this field raises NotImplementedError at db_type time on SQLite so you find out at migrate, not at query.

Example::

class Article(dorm.Model):
    tags = dorm.ArrayField(dorm.CharField(max_length=50), null=True)

Usage::

Article.objects.create(tags=["python", "orm"])
# Filter for membership (PG ``ANY`` operator) — use ``__contains``:
Article.objects.filter(tags__contains=["python"])

dorm.fields.IPAddressField

Bases: Field[str]

dorm.fields.GenericIPAddressField

Bases: Field[str]

dorm.fields.AutoField

Bases: Field[int]

dorm.fields.BigAutoField

Bases: AutoField

dorm.fields.SmallAutoField

Bases: AutoField

dorm.fields.ForeignKey

Bases: RelatedField

dorm.fields.OneToOneField

Bases: RelatedField

dorm.fields.ManyToManyField

Bases: Field[Any]