constraints.txt 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. =====================
  2. Constraints reference
  3. =====================
  4. .. module:: django.db.models.constraints
  5. .. currentmodule:: django.db.models
  6. .. versionadded:: 2.2
  7. The classes defined in this module create database constraints. They are added
  8. in the model :attr:`Meta.constraints <django.db.models.Options.constraints>`
  9. option.
  10. .. admonition:: Referencing built-in constraints
  11. Constraints are defined in ``django.db.models.constraints``, but for
  12. convenience they're imported into :mod:`django.db.models`. The standard
  13. convention is to use ``from django.db import models`` and refer to the
  14. constraints as ``models.<Foo>Constraint``.
  15. ``CheckConstraint``
  16. ===================
  17. .. class:: CheckConstraint(*, check, name)
  18. Creates a check constraint in the database.
  19. ``check``
  20. ---------
  21. .. attribute:: CheckConstraint.check
  22. A :class:`Q` object that specifies the check you want the constraint to
  23. enforce.
  24. For example ``CheckConstraint(check=Q(age__gte=18), name='age_gte_18')``
  25. ensures the age field is never less than 18.
  26. ``name``
  27. --------
  28. .. attribute:: CheckConstraint.name
  29. The name of the constraint.