Добавление дополнительных полей не работает в моей форме Django - PullRequest
1 голос
/ 20 августа 2010

У меня есть модель с пользовательским свойством

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)

Я пытаюсь отобразить свойство 'json_data' в моем шаблоне.

У меня есть этот код в моей форме

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

Я знаю, что данные в моем свойстве 'json_data' недействительны, но я не могу увидеть данные этого свойства в моем шаблоне

На мой взгляд, у меня есть этот кусок кода

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)

Полагаю, мой вопрос: как отобразить данные из дополнительного поля модели?

1 Ответ

0 голосов
/ 20 августа 2010

передать данные дополнительных полей в качестве исходных данных в форму

initial_data = [{'json_data': objRecipe.json_data}]
recipeIngredients = RecipeIngredientFormSet(initial=initial_data, instance = objRecipe)

для правильного использования исходных данных см. с использованием исходных данных с набором форм

...