8👍
✅
You can have the unique_together on the "through" table (in your case the Avaliability Table)
class Availability(models.Model):
player = models.ForeignKey(Player, on_delete=models.CASCADE)
hour = models.ForeignKey(Hour, on_delete=models.CASCADE)
available = models.BooleanField()
class Meta:
unique_together = (('player','hour'),)
And then use the get_or_create for adding/modifying:
obj,created = Availability.objects.get_or_create(hour=hour,player=player)
1👍
I’ve worked around it modifying save()
for Availability
:
def save(self, *args, **kwargs):
if self.pk is None:
combination = Availability.objects.filter(player=self.player,
hour=self.hour).first()
if combination:
self.pk = combination.pk
super(Availability, self).save(*args, **kwargs)
Source:stackexchange.com