You can model properties in a template.

I want to show a model property in a template that uses inlineformset_factory. Is it possible? I have not seen a single 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 can’t see anything

 {% for form in recipeIngredients %} {{ form.json_data }} {% endfor %} 
+6
django django-models
source share
1 answer

Yes, you can access properties such as access to any other model variable. But you are printing a form here, not an instance.

If you use form.instance.json_data , it will work.

+7
source share

All Articles