我可以在unicode返回中使用外键吗?

2024-05-17 12:15:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下的课程:配料,配方和沉淀物。。。

class Ingredient(models.Model):
    name = models.CharField(max_length=30, primary_key=True)
    qty_on_stock = models.IntegerField()

    def __unicode__(self):
        return self.name

class Recipe(models.Model):
    name = models.CharField(max_length=30, primary_key=True)
    comments = models.TextField(blank=True)
    ingredient = models.ManyToManyField(Ingredient)

    def __unicode__(self):
        return self.name

class RecipeContent(models.Model):
    recipe = models.ForeignKey(Recipe)
    ingredients = models.ForeignKey(Ingredient)
    qty_used = models.IntegerField()

但对于RecipeContent中的unicode,我想使用此RecipeContent所属的配方名称。。。有办法吗?


Tags: keynameselftruemodelmodelsunicode配方