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.
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)
in 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.
If you use form.instance.json_data
it will work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With