1👍
✅
One simple way would probably be handling it in the code.
Before save, you check if object exists in ManyToMany
field
You can do it in one of 2 ways i can think of in the models:
Create a new table that references the User
, with the Suggestion
and the Event
OR
Have a unique property on the through
table
class Event(models.Model):
def __unicode__(self):
return self.title
event_id = models.BigIntegerField(blank = 'TRUE', primary_key='TRUE')
title = models.CharField(max_length=200, blank = 'TRUE')
start = models.CharField(max_length=200, blank = 'TRUE', null = 'TRUE')
end = models.CharField(max_length=200,blank = 'TRUE', null = 'TRUE')
location = models.CharField(max_length=200, blank = 'TRUE', null = 'TRUE')
tags = models.CharField(max_length=200, blank = 'TRUE', null = 'TRUE')
url = models.CharField(max_length=200, blank = 'TRUE', null = 'TRUE')
suggested = models.ManyToManyField('suggestedName', blank = 'TRUE', null = 'TRUE', through='EventSuggestion')
class EventSuggestion(models.Model):
#through table attributes
event = models.ForeignKey(Event)
suggestion = models.ForeignKey(suggestedName)
user = models.ForeignKey('User')
class Meta:
unique_together = ('event', 'suggestion', 'user')
Source:stackexchange.com