【Python】 Django 怎么實現 聯合主鍵?


unique_together

Options. unique_together

Sets of field names that, taken together, must be unique:

unique_together = (("driver", "restaurant"),) 

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 appropriate UNIQUE statements are included in the CREATE TABLEstatement).

For convenience, unique_together can be a single tuple when dealing with a single set of fields:

unique_together = ("driver", "restaurant") 

ManyToManyField cannot be included in unique_together. (It’s not clear what that would even mean!) If you need to validate uniqueness related to a ManyToManyField, try using a signal or an explicit through model.

The ValidationError raised during model validation when the constraint is violated has the unique_together error code.

 

 

參考資料:

https://docs.djangoproject.com/en/dev/ref/models/options/#unique-together

http://m.blog.csdn.net/article/details?id=8768504

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM