2010-08-21 17 views
6

में प्रदर्शित किया जा सकता है मैं एक टेम्पलेट में एक मॉडल प्रॉपर्टी प्रदर्शित करना चाहता हूं, जो इनलाइनफॉर्मसेट_फैक्टरी का उपयोग करता है। क्या यह भी संभव है? मैं किसी भी उदाहरण में नहीं आया है।मॉडल गुणों को टेम्पलेट

मैं अपने टेम्पलेट में 'json_data' प्रदर्शित करने के लिए views.py

RecipeIngredientFormSet = inlineformset_factory(models.Recipe, models.RecipeIngredient, form=forms.RecipeIngredientForm, extra=0) 
recipeIngredients = RecipeIngredientFormSet(instance = objRecipe) 

मेरे टेम्पलेट में

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) 

कोशिश कर रहा हूँ, मैं इस है, लेकिन मैं कुछ भी नहीं दिख रहा है

{% for form in recipeIngredients %} 
{{ form.json_data }} 
{% endfor %} 

उत्तर

7

हाँ आप किसी अन्य मॉडल चर का उपयोग कर सकते हैं जैसे गुणों तक पहुंच सकते हैं। लेकिन आप यहां एक फॉर्म प्रिंट कर रहे हैं, उदाहरण नहीं।

यदि आप form.instance.json_data का उपयोग करते हैं तो यह काम करेगा।

+0

शांत, धन्यवाद। मुझे घटना के बारे में पता नहीं था। – iJK

+0

मैं हर जगह यह जवाब ढूंढ रहा हूं! बहुत बहुत धन्यवाद, बहुत, WoLPH! – Bobort

संबंधित मुद्दे