0
我使用Python + Django和這個在我的模型現在:如何限制多對多關係的選擇?
class Team(models.Model):
player = models.ManyToManyField(Player, related_name="player", through="Team_Player")
squad = models.ManyToManyField(Player, related_name="squad", blank=True)
class Player(Person):
name = models.CharField(max_length=100)
class Team_Player(models.Model):
team = models.ForeignKey(Team)
player = models.ForeignKey(Player)
有沒有辦法來限制Team.squad只具有內部Team.player關係的球員?到目前爲止,我試圖使用limit_choices_to而沒有運氣。任何線索?
我想你可能會在你的Team_Player模型中有一些冗餘。你能描述一下你試圖完成什麼嗎? – dm03514
模型結構看起來很雜亂。除此之外 - 您可以覆蓋save()方法或使用https://docs.djangoproject.com/en/dev/ref/signals/#django.db.models.signals.pre_save PRE-SAVE信號來檢查小隊中只有隊員,並且如果不匹配的話會引發ValueError。 – Tisho
我相信你在尋找這樣的: http://stackoverflow.com/questions/4621494/django-limiting-the-number-of-relationships-in-a-onetomany-relationship – Medeiros