Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding extra fields not working in my django form
    text
    copied!<p>I have a model with a custom property</p> <pre><code>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) </code></pre> <p>I am trying to display the property 'json_data' in my template. </p> <p>I have this piece of code in my form</p> <pre><code>class RecipeIngredientForm(forms.ModelForm): json_data = forms.CharField(widget=forms.HiddenInput()) def __init__(self, *args, **kwargs): super(RecipeIngredientForm, self).__init__(*args, **kwargs) print('here') if kwargs.has_key('instance'): instance = kwargs['instance'] print(instance) self.initial['json_data'] = instance.json_data </code></pre> <p>I know the data in my property 'json_data' is not valid, but I am unable to see the data from this property in my template. </p> <p>In my views, I have this piece of code</p> <pre><code>RecipeIngredientFormSet = inlineformset_factory(models.Recipe, models.RecipeIngredient, form=forms.RecipeIngredientForm, extra=0) recipe_id = int(id) objRecipe = models.Recipe.objects.get(id=recipe_id) recipe = forms.RecipeForm(instance=objRecipe) recipeIngredients = RecipeIngredientFormSet(instance = objRecipe) </code></pre> <p>I guess my question is how do I display data from an extra model field?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload