47👍
The unique_together
attribute of the Meta
class of your model is what you are looking for:
class Meta:
unique_together = ('poll', 'user_id')
Check django docs for more information.
- [Django]-AssertionError: database connection isn't set to UTC
- [Django]-Django error: render_to_response() got an unexpected keyword argument 'context_instance'
- [Django]-Django Rest framework, how to include '__all__' fields and a related field in ModelSerializer ?
41👍
Django 2.2 introduced UniqueConstraint
and the note in the official documentation on this topic suggests that unique_together
might be deprecated in future. See deprecation note here.
You can add UniqueConstraint
to the Meta.constraints
option of your model class like so:
class Meta:
constraints = [
models.UniqueConstraint(fields=['poll', 'user_id'], name="user-polled")
]
- [Django]-Profiling Django
- [Django]-New url format in Django 1.9
- [Django]-When should I use a custom Manager versus a custom QuerySet in Django?
2👍
You want the unique_together
attribute:
https://docs.djangoproject.com/en/dev/ref/models/options/#unique-together
- [Django]-Display only some of the page numbers by django pagination
- [Django]-Django migration fails with "__fake__.DoesNotExist: Permission matching query does not exist."
- [Django]-Django: get table name of a model in the model manager?
Source:stackexchange.com