ValueError:无法将字段Schedule.Swimming.player更改为Schedule.Swimming.player(您无法更改M2M字段)

时间:2018-07-06 12:58:37

标签: python django django-models

class Match(models.Model):

    date = models.DateField('my date')
    time = models.TimeField()
    duration = models.DurationField(null = True)
    category = models.CharField(max_length=20, choices=CATEGORY_CHOICES, default=CATEGORY_CHOICES[0][0])
    place = models.CharField(max_length=256, null=True)
    game_status = models.CharField(max_length=256, null=True) #completed or not

    # Foreign Keys
    event = models.ForeignKey(Event, on_delete=models.CASCADE)
    # teams = models.ManyToManyField(Team)

    class Meta:
        abstract = True   

 class Swimming(Match):
        time = models.TimeField(null=True)
        player = models.ManyToManyField(Player, related_name='player_swimming')
        game_level = models.CharField(max_length=256, null=True, choices=LEVEL_CHOICES) # like semi-final, final etc
        game_specific = models.CharField(max_length=256,null=True, choices=EVENT_CHOICES) #like Men's Shot Put or Men's Triple Jump etc

        def __str__(self):
            return str(self.game_level)

这是我的初始模型游泳,它是从基类 Match 扩展而来的。现在我想在 Swimming 模型中添加一个中间模型 PlayerSwimming 。现在它在makemigration阶段卡住了。我尝试了所有可能性,例如首先删除迁移并再次添加,注释和迁移,然后取消注释并再次迁移。帮我!新模型是:

class Swimming(Match):


    player = models.ManyToManyField(Player, through='PlayerSwimming')
    game_level = models.CharField(max_length=256, null=True, choices=LEVEL_CHOICES) # like semi-final, final etc
    game_specific = models.CharField(max_length=256,null=True, choices=EVENT_CHOICES) #like Men's Shot Put or Men's Triple Jump etc


    def __str__(self):
        return str(str(self.category) + "  " + str(self.date))


class PlayerSwimming(models.Model):

    player = models.ForeignKey(Player, on_delete=models.CASCADE)
    swimming = models.ForeignKey(Swimming, on_delete=models.CASCADE, default='')
    time = models.IntegerField(default=-1)
    medal = models.CharField(max_length=256,blank=True, choices=MEDAL_CHOICES)

    def __str__(self):
        return str(self.player)
        # return str(self.swimming) + str(" ") + str(self.player)

    class Meta:
        ordering = ['time']

1 个答案:

答案 0 :(得分:0)

我认为您的钥匙太多。这应该给您您想要的东西。

class Swimming(Match):
    game_level = models.CharField(max_length=256, null=True, choices=LEVEL_CHOICES)
    game_specific = models.CharField(max_length=256, null=True, choices=EVENT_CHOICES)

    def __str__(self):
        return str(str(self.category) + "  " + str(self.date))


class PlayerSwimming(models.Model):

    player = models.ForeignKey(Player, on_delete=models.CASCADE)
    swimming = models.ForeignKey(Swimming, on_delete=models.CASCADE, default='')
    time = models.IntegerField(default=-1)
    medal = models.CharField(max_length=256,blank=True, choices=MEDAL_CHOICES)

    def __str__(self):
        return str(self.player)
        # return str(self.swimming) + str(" ") + str(self.player)

    class Meta:
        ordering = ['time']