Constraints reference

    The classes defined in this module create database constraints. They are addedin the model option.

    Constraints are defined in django.db.models.constraints, but forconvenience they're imported into . The standardconvention is to use from django.db import models and refer to theconstraints as models.<Foo>Constraint.

    • class CheckConstraint(*, check, name)[源代码]
    • Creates a check constraint in the database.
    • CheckConstraint.
    • A object that specifies the check you want the constraint toenforce.
    • CheckConstraint.name
    • The name of the constraint.

    UniqueConstraint

    • Creates a unique constraint in the database.
    • UniqueConstraint.
    • A list of field names that specifies the unique set of columns you want theconstraint to enforce.

    For example, UniqueConstraint(fields=['room', 'date'],
    name='unique_booking')
    ensures each room can only be booked once for eachdate.

    • UniqueConstraint.name
    • The name of the constraint.
    • UniqueConstraint.condition

    These conditions have the same database restrictions as.