33👍
✅
Yes, use unique_together:
class Like(models.Model):
user = models.ForeignKey(User)
post = models.ForeignKey(Post)
time = models.DateTimeField(auto_now_add=True)
class Meta:
unique_together = ('user', 'post')
👤knbk
20👍
unique_together
will be deprecated in the future version, instead you could apply UniqueConstraint
. This and this link gives example code.
class Like(models.Model):
user = models.ForeignKey(User)
post = models.ForeignKey(Post)
time = models.DateTimeField(auto_now_add=True)
class Meta:
constraints = [
models.UniqueConstraint(fields=['user', 'post'], name='unique_user_post'),
]
👤Ham
- [Django]-Django/DRF – 405 Method not allowed on DELETE operation
- [Django]-Negating a boolean in Django template
- [Django]-Serializer call is showing an TypeError: Object of type 'ListSerializer' is not JSON serializable?
Source:stackexchange.com