模型属性可以显示在模板中 [英] Can model properties be displayed in a template
本文介绍了模型属性可以显示在模板中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找在模板中显示一个模型属性,它使用inlineformset_factory。这是甚么可能吗?我没有碰到任何一个例子。
I am looking to display a model property in a template, which uses an inlineformset_factory. Is this even possible? I have not come across any example.
我试图在我的模板中显示'json_data'
I am trying to display 'json_data' in my template
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)
在我的温度板,我有这个,但我没有看到任何东西
In my template, I have this but I don't see anything
{% for form in recipeIngredients %}
{{ form.json_data }}
{% endfor %}
推荐答案
是的,您可以访问属性,如您可以访问任何其他模型变量。但是你在这里打印一个表单,而不是一个实例。
Yes you can access properties like you can access any other model variable. But you are printing a form here, not an instance.
如果你使用 form.instance.json_data
工作。
这篇关于模型属性可以显示在模板中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文