Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can model properties be displayed in a template

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 %}
like image 349
iJK Avatar asked Aug 21 '10 02:08

iJK


1 Answers

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.

like image 112
Wolph Avatar answered Sep 20 '22 11:09

Wolph