constraints.txt 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. Instead, specify the ``constraints`` option
  21. on subclasses directly, providing a unique name for each constraint.
  22. .. admonition:: Validation of Constraints
  23. In general constraints are **not** checked during ``full_clean()``, and do
  24. not raise ``ValidationError``\s. Rather you'll get a database integrity
  25. error on ``save()``. ``UniqueConstraint``\s are different in this regard,
  26. in that they leverage the existing ``validate_unique()`` logic, and thus
  27. enable two-stage validation. In addition to ``IntegrityError`` on
  28. ``save()``, ``ValidationError`` is also raised during model validation when
  29. the ``UniqueConstraint`` is violated.
  30. ``CheckConstraint``
  31. ===================
  32. .. class:: CheckConstraint(*, check, name)
  33. Creates a check constraint in the database.
  34. ``check``
  35. ---------
  36. .. attribute:: CheckConstraint.check
  37. A :class:`Q` object that specifies the check you want the constraint to
  38. enforce.
  39. For example, ``CheckConstraint(check=Q(age__gte=18), name='age_gte_18')``
  40. ensures the age field is never less than 18.
  41. ``name``
  42. --------
  43. .. attribute:: CheckConstraint.name
  44. The name of the constraint.
  45. ``UniqueConstraint``
  46. ====================
  47. .. class:: UniqueConstraint(*, fields, name, condition=None)
  48. Creates a unique constraint in the database.
  49. ``fields``
  50. ----------
  51. .. attribute:: UniqueConstraint.fields
  52. A list of field names that specifies the unique set of columns you want the
  53. constraint to enforce.
  54. For example, ``UniqueConstraint(fields=['room', 'date'],
  55. name='unique_booking')`` ensures each room can only be booked once for each
  56. date.
  57. ``name``
  58. --------
  59. .. attribute:: UniqueConstraint.name
  60. The name of the constraint.
  61. ``condition``
  62. -------------
  63. .. attribute:: UniqueConstraint.condition
  64. A :class:`Q` object that specifies the condition you want the constraint to
  65. enforce.
  66. For example::
  67. UniqueConstraint(fields=['user'], condition=Q(status='DRAFT'), name='unique_draft_user')
  68. ensures that each user only has one draft.
  69. These conditions have the same database restrictions as
  70. :attr:`Index.condition`.