constraints.txt 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. ``CheckConstraint``
  23. ===================
  24. .. class:: CheckConstraint(*, check, name)
  25. Creates a check constraint in the database.
  26. ``check``
  27. ---------
  28. .. attribute:: CheckConstraint.check
  29. A :class:`Q` object that specifies the check you want the constraint to
  30. enforce.
  31. For example, ``CheckConstraint(check=Q(age__gte=18), name='age_gte_18')``
  32. ensures the age field is never less than 18.
  33. ``name``
  34. --------
  35. .. attribute:: CheckConstraint.name
  36. The name of the constraint.
  37. ``UniqueConstraint``
  38. ====================
  39. .. class:: UniqueConstraint(*, fields, name, condition=None)
  40. Creates a unique constraint in the database.
  41. ``fields``
  42. ----------
  43. .. attribute:: UniqueConstraint.fields
  44. A list of field names that specifies the unique set of columns you want the
  45. constraint to enforce.
  46. For example, ``UniqueConstraint(fields=['room', 'date'],
  47. name='unique_booking')`` ensures each room can only be booked once for each
  48. date.
  49. ``name``
  50. --------
  51. .. attribute:: UniqueConstraint.name
  52. The name of the constraint.
  53. ``condition``
  54. -------------
  55. .. attribute:: UniqueConstraint.condition
  56. A :class:`Q` object that specifies the condition you want the constraint to
  57. enforce.
  58. For example::
  59. UniqueConstraint(fields=['user'], condition=Q(status='DRAFT'), name='unique_draft_user')
  60. ensures that each user only has one draft.
  61. These conditions have the same database restrictions as
  62. :attr:`Index.condition`.