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!
Share
You can use unique-together in the
Metaclass.