unique_together
¶
unique_together
-
Options.
unique_together
- Sets of field names that, taken together, must be unique:
This is a tuple of tuples that must be unique when considered together. It’s used in the Django admin and is enforced at the database level (i.e., the appropriateunique_together = (("driver", "restaurant"),)
statements are included in theUNIQUE
CREATE TABLE
statement).
For convenience, unique_together can be a single tuple when dealing with a single set of fields:
Aunique_together = ("driver", "restaurant")
cannot be included in unique_together. (It’s not clear what that would even mean!) If you need to validate uniqueness related to aManyToManyField
, try using a signal or an explicitManyToManyField
through
model.
The
raised during model validation when the constraint is violated has theValidationError
unique_together
error code.
参考资料:
https://docs.djangoproject.com/en/dev/ref/models/options/#unique-together
http://m.blog.csdn.net/article/details?id=8768504