我已经成功地使用内联表单集创建了一个配方输入表单,该表单由一个配方表单(只是一个模型表单)和一个 RecipeIngredient 表单集组成。这些型号是:
#models.py
class Recipe(models.Model):
title = models.CharField(max_length=255)
description = models.TextField(blank=True)
directions = models.TextField()
class RecipeIngredient(models.Model):
quantity = models.DecimalField(max_digits=5, decimal_places=3)
unit_of_measure = models.CharField(max_length=10, choices=UNIT_CHOICES)
ingredient = models.CharField(max_length=100, choices=INGREDIENT_CHOICES)
recipe = models.ForeignKey(Recipe)
我想将成分更改为以下内容:
ingredient = models.ForeignKey(Ingredient)
其中成分是:
class Ingredient(models.Model):
title = models.CharField(max_length=100)
我没有改变 views.py 来设置内联表单集:
FormSet = inlineformset_factory(Recipe, RecipeIngredient, extra=1,
can_delete=False)
一切都很完美......直到我点击了配料下拉菜单,只看到每个配料条目重复的“配料对象”选项,而不是我正在寻找的标题值。
有什么方法可以保持这种直截了当的方法并在下拉菜单中显示 Ingredient.title 吗?保存、显示等是否会有其他问题?
如果做不到这一点,我需要做什么才能完成这项工作?
谢谢大家。