constraints.txt 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. =====================
  2. Constraints reference
  3. =====================
  4. .. module:: django.db.models.constraints
  5. .. currentmodule:: django.db.models
  6. The classes defined in this module create database constraints. They are added
  7. in the model :attr:`Meta.constraints <django.db.models.Options.constraints>`
  8. option.
  9. .. admonition:: Referencing built-in constraints
  10. Constraints are defined in ``django.db.models.constraints``, but for
  11. convenience they're imported into :mod:`django.db.models`. The standard
  12. convention is to use ``from django.db import models`` and refer to the
  13. constraints as ``models.<Foo>Constraint``.
  14. .. admonition:: Constraints in abstract base classes
  15. You must always specify a unique name for the constraint. As such, you
  16. cannot normally specify a constraint on an abstract base class, since the
  17. :attr:`Meta.constraints <django.db.models.Options.constraints>` option is
  18. inherited by subclasses, with exactly the same values for the attributes
  19. (including ``name``) each time. To work around name collisions, part of the
  20. name may contain ``'%(app_label)s'`` and ``'%(class)s'``, which are
  21. replaced, respectively, by the lowercased app label and class name of the
  22. concrete model. For example ``CheckConstraint(check=Q(age__gte=18),
  23. name='%(app_label)s_%(class)s_is_adult')``.
  24. .. admonition:: Validation of Constraints
  25. In general constraints are **not** checked during ``full_clean()``, and do
  26. not raise ``ValidationError``\s. Rather you'll get a database integrity
  27. error on ``save()``. ``UniqueConstraint``\s without a
  28. :attr:`~UniqueConstraint.condition` (i.e. non-partial unique constraints)
  29. are different in this regard, in that they leverage the existing
  30. ``validate_unique()`` logic, and thus enable two-stage validation. In
  31. addition to ``IntegrityError`` on ``save()``, ``ValidationError`` is also
  32. raised during model validation when 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`.