我希望在模板中显示模型属性,该属性使用 inlineformset_factory。这可能吗?我还没有遇到过任何例子。
我正在尝试在模板中显示“json_data”
class RecipeIngredient(models.Model):
recipe = models.ForeignKey(Recipe)
ingredient = models.ForeignKey(Ingredient)
serving_size = models.ForeignKey(ServingSize)
quantity = models.IntegerField()
order = models.IntegerField()
created = models.DateTimeField(auto_now_add = True)
updated = models.DateTimeField(auto_now = True)
def _get_json_data(self):
return u'%s %s' % (self.id, self.ingredient.name)
json_data = property(_get_json_data)
在views.py中
RecipeIngredientFormSet = inlineformset_factory(models.Recipe, models.RecipeIngredient, form=forms.RecipeIngredientForm, extra=0)
recipeIngredients = RecipeIngredientFormSet(instance = objRecipe)
在我的模板中,我有这个,但我没有看到任何东西
{% for form in recipeIngredients %}
{{ form.json_data }}
{% endfor %}
是的,您可以像访问任何其他模型变量一样访问属性。但您在这里打印的是表单,而不是实例。
如果你使用form.instance.json_data
它会起作用的。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)