开发者

Multi-Value Primary Keys in Django

开发者 https://www.devze.com 2023-04-03 01:25 出处:网络
I need to enforce a property in a model such that for each \'User\' field in this particular table, it\'s corresponding \'Post\' field is distinct - and vice versa (i.e., for each particular \'Post\'

I need to enforce a property in a model such that for each 'User' field in this particular table, it's corresponding 'Post' field is distinct - and vice versa (i.e., for each particular 'Post' field, the corresponding 'User' field is distinct). I'm not sure how to do this in Django. I think one approach would be to define the ('User', 'Post') tuple to be a primary key, but it doesn't seem that Django supports this. Is there any other 开发者_如何学编程way to enforce this constraint at the field specifications of a Django model (i.e., without doing all the checks in a save() function)? Thanks, appreciate any feedback!


You can use unique-together in the Meta class.

0

精彩评论

暂无评论...
验证码 换一张
取 消