constraints.txt 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. .. admonition:: Constraints in abstract base classes
  16. You must always specify a unique name for the constraint. As such, you
  17. cannot normally specify a constraint on an abstract base class, since the
  18. :attr:`Meta.constraints <django.db.models.Options.constraints>` option is
  19. inherited by subclasses, with exactly the same values for the attributes
  20. (including ``name``) each time. To work around name collisions, part of the
  21. name may contain ``'%(app_label)s'`` and ``'%(class)s'``, which are
  22. replaced, respectively, by the lowercased app label and class name of the
  23. concrete model. For example ``CheckConstraint(check=Q(age__gte=18),
  24. name='%(app_label)s_%(class)s_is_adult')``.
  25. .. admonition:: Validation of Constraints
  26. In general constraints are **not** checked during ``full_clean()``, and do
  27. not raise ``ValidationError``\s. Rather you'll get a database integrity
  28. error on ``save()``. ``UniqueConstraint``\s are different in this regard,
  29. in that they leverage the existing ``validate_unique()`` logic, and thus
  30. enable two-stage validation. In addition to ``IntegrityError`` on
  31. ``save()``, ``ValidationError`` is also raised during model validation when
  32. the ``UniqueConstraint`` is violated.
  33. ``CheckConstraint``
  34. ===================
  35. .. class:: CheckConstraint(*, check, name)
  36. Creates a check constraint in the database.
  37. ``check``
  38. ---------
  39. .. attribute:: CheckConstraint.check
  40. A :class:`Q` object that specifies the check you want the constraint to
  41. enforce.
  42. For example, ``CheckConstraint(check=Q(age__gte=18), name='age_gte_18')``
  43. ensures the age field is never less than 18.
  44. ``name``
  45. --------
  46. .. attribute:: CheckConstraint.name
  47. The name of the constraint.
  48. .. versionchanged:: 3.0
  49. Interpolation of ``'%(app_label)s'`` and ``'%(class)s'`` was added.
  50. ``UniqueConstraint``
  51. ====================
  52. .. class:: UniqueConstraint(*, fields, name, condition=None)
  53. Creates a unique constraint in the database.
  54. ``fields``
  55. ----------
  56. .. attribute:: UniqueConstraint.fields
  57. A list of field names that specifies the unique set of columns you want the
  58. constraint to enforce.
  59. For example, ``UniqueConstraint(fields=['room', 'date'],
  60. name='unique_booking')`` ensures each room can only be booked once for each
  61. date.
  62. ``name``
  63. --------
  64. .. attribute:: UniqueConstraint.name
  65. The name of the constraint.
  66. .. versionchanged:: 3.0
  67. Interpolation of ``'%(app_label)s'`` and ``'%(class)s'`` was added.
  68. ``condition``
  69. -------------
  70. .. attribute:: UniqueConstraint.condition
  71. A :class:`Q` object that specifies the condition you want the constraint to
  72. enforce.
  73. For example::
  74. UniqueConstraint(fields=['user'], condition=Q(status='DRAFT'), name='unique_draft_user')
  75. ensures that each user only has one draft.
  76. These conditions have the same database restrictions as
  77. :attr:`Index.condition`.