check-constraints.txt 1.2 KB

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