Django模型——如何使一个模型中的字段在另一个模型(即外键)上可靠?

时间:2021-11-26 06:45:14

I'm new to Django and I'm not sure how to deal with folowing problem:

我是Django的新手,不知道如何处理folowing问题:

I have models:

我有模型:

class Season(models.Model):
    number_of_episodes = models.IntegerField()
class Episode(models.Model):
    season = models.ForeignKey(Season)
    number = models.IntegerField()

Episode number should not only be unique in season but also smaller than season's number_of_episodes.

每一集的数量不仅应该是唯一的,而且应该小于每一季的数量。

1 个解决方案

#1


2  

To get the episode number to be unique in season, you can use unique_together. This means that to for every value of one of the field, there can be only one row with a value in the other field. That would look about this:

要使每一集的数量在季中是唯一的,您可以使用unique_together。这意味着对于一个字段的每个值,在另一个字段中只能有一行的值。这将会是这样的:

class Season(models.Model):
    number_of_episodes = models.IntegerField()
class Episode(models.Model):
    class Meta:
        unique_together = (season, number)
    season = models.ForeignKey(Season)
    number = models.IntegerField()

I don't think you can use django models to enforce that the episode number should be less than season.number_of_episodes, but you can do that in a overridden save-method. Something like this:

我不认为你可以用django模型来强调这一集的数量应该小于季数。number_of_episodes,但是可以在重写的保存方法中执行。是这样的:

class Season(models.Model):
    number_of_episodes = models.IntegerField()

class Episode(models.Model):
    class Meta:
        unique_together = (season, number)
    season = models.ForeignKey(Season)
    number = models.IntegerField()

    def save(self, *args, **kwargs):
        if self.number > season.number_of_episodes:
            return #Don't save
        else:
            super(Episode, self).save(*args, **kwargs) # Call the "real" save() method.

Note that this only jumps out of the save-method if the episode-number is incorrect. You will probably raise a exception instead.

注意,只有在情景数不正确的情况下,这才会跳出save方法。你可能会提出一个例外。

#1


2  

To get the episode number to be unique in season, you can use unique_together. This means that to for every value of one of the field, there can be only one row with a value in the other field. That would look about this:

要使每一集的数量在季中是唯一的,您可以使用unique_together。这意味着对于一个字段的每个值,在另一个字段中只能有一行的值。这将会是这样的:

class Season(models.Model):
    number_of_episodes = models.IntegerField()
class Episode(models.Model):
    class Meta:
        unique_together = (season, number)
    season = models.ForeignKey(Season)
    number = models.IntegerField()

I don't think you can use django models to enforce that the episode number should be less than season.number_of_episodes, but you can do that in a overridden save-method. Something like this:

我不认为你可以用django模型来强调这一集的数量应该小于季数。number_of_episodes,但是可以在重写的保存方法中执行。是这样的:

class Season(models.Model):
    number_of_episodes = models.IntegerField()

class Episode(models.Model):
    class Meta:
        unique_together = (season, number)
    season = models.ForeignKey(Season)
    number = models.IntegerField()

    def save(self, *args, **kwargs):
        if self.number > season.number_of_episodes:
            return #Don't save
        else:
            super(Episode, self).save(*args, **kwargs) # Call the "real" save() method.

Note that this only jumps out of the save-method if the episode-number is incorrect. You will probably raise a exception instead.

注意,只有在情景数不正确的情况下,这才会跳出save方法。你可能会提出一个例外。