Ero*_*San 12 python django django-models
我有以下课程:成分,食谱和食谱内容......
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()
Run Code Online (Sandbox Code Playgroud)
但是对于RecipeContent中的__unicode __(),我想使用这个RecipeContent所属的Recipe名称...有没有办法做到这一点?
jb.*_*jb. 26
class RecipeContent(models.Model):
...
def __unicode__(self):
# You can access ForeignKey properties through the field name!
return self.recipe.name
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5210 次 |
| 最近记录: |