我有两个具有多对多关系的模型。这是代码:
class Player(models.Model):
first_name = models.CharField(max_length = 30, verbose_name = u"First name")
last_name = models.CharField(max_length = 50, verbose_name = u"Last name")
def __unicode__(self):
return "%s %s" % (self.last_name, self.first_name)
class Tournament(models.Model):
title = models.CharField(max_length = 100, verbose_name = u"Tournament's title")
players = models.ManyToManyField(Player,verbose_name = u"Tournament's players")
def __unicode__(self):
return self.title
def save(self, **kwargs):
Tournament(title = self.title)
all_players = Player.objects.all()
for member in all_players:
member_of_tournament = member.tournament_set.filter(title = self.title)
for j in member_of_tournament:
print member.tournament_set.filter(title = self.title)
self.players.add(member)
super(Tournament, self).save(**kwargs)
当我第一次保存锦标赛时,它只保存标题。但是当我下次保存时,它也会保存玩家并将他们与锦标赛相关联。我怎样才能在比赛的同时保存它们?