1

I have a problem with the initial value of a hidden field in a modelform deployed in a formset. Just the first form have the initial value in the hidden field and the others are empty.

The modelForm is:

class md_changelogFormModel(forms.ModelForm):
    post_docs = forms.CharField(widget=forms.HiddenInput(),
                                initial="READY_FOR_RUN")
    # Other fields

    class Meta:
        model = md_changelog
        fields = '__all__'

The same model md_changelog is used in a form based in BaseInlineFormSet:

class md_changelogForm(BaseInlineFormSet)

      class Meta:
            model = md_changelog
            fields = '__all__'

And a formset class is created with inlineformset_factory:

md_changelogFormSet = inlineformset_factory(md_orderlog, md_changelog,
                                            form=md_changelogFormModel,
                                            formset=md_changelogForm,
                                            extra=1, max_num=20)

And the formset class is instantiated in a class based view:

def get(self, request, *args, **kwargs):
    """
    Handles GET requests and instantiates blank versions of the form
    and its inline formsets.
    """
    self.object = None
    form_class = self.get_form_class()
    form = self.get_form(form_class)

    md_changelog_form = md_changelogFormSet(initial=[{'post_docs':'READY_FOR_RUN'}])

    return self.render_to_response(
        self.get_context_data(form=form,
                              md_changelog_form=md_changelog_form))

The problem is that when the post function receives the request.POST, only the post_docs hidden field in the first form of the formset data is filled with the initial value, the others no.

How can I initialize the other forms?

Thanks!

Pedro Muñoz
  • 285
  • 3
  • 16

1 Answers1

0

If you need to provide initial values for all forms in a given form-field please have a look at the docs: https://docs.djangoproject.com/en/1.8/topics/forms/formsets/#using-initial-data-with-a-formset

Hence, in my view in order to initialize all 3 fields with the same initial date you should change your code to:

def get(self, request, *args, **kwargs):
    """
    Handles GET requests and instantiates blank versions of the form
    and its inline formsets.
    """
    self.object = None
    form_class = self.get_form_class()
    form = self.get_form(form_class)

   md_changelog_form = md_changelogFormSet(initial=[
       {'post_docs':'READY_FOR_RUN'},
       {'post_docs':'READY_FOR_RUN'},
       {'post_docs':'READY_FOR_RUN'},
   ])

   return self.render_to_response(self.get_context_data(form=form, md_changelog_form=md_changelog_form))

Hope that helps!

user1544500
  • 2,067
  • 5
  • 25
  • 35
  • Thanks!, but in the form we add new forms with the jQuery Formset pluguin, in that case we will need to assign the hidden field value with javascript? – Pedro Muñoz Oct 01 '15 at 20:54
  • 1
    I don't have a possibility to test it but potientially you can change how a new field in a given formset is initialized. Have a look at this question - [http://stackoverflow.com/questions/1500121/django-how-to-set-initial-values-for-a-field-in-an-inline-model-formset](http://stackoverflow.com/questions/1500121/django-how-to-set-initial-values-for-a-field-in-an-inline-model-formset) - this should solve your problem. – user1544500 Oct 01 '15 at 21:18