0

I have following definition for ModelFormset:

class MyModelDetailsForm(forms.ModelForm):
    status = forms.ChoiceField(choices=MyModel.STATUS)

    class Meta:
        model = MyModel
        fields = ['id']


MyModelDetailsFormset = modelformset_factory(
    MyModel,
    form=MyModelDetailsForm,
    extra=0)

And this is how I'm creating instance in get_context_data

formset = MyModelDetailsFormset(queryset=self.my_models)

If I'll initialize status fields this way:

for form in formset:
    form.fields['status'].initial = get_initial_value()

everything works.

If I'd like to do something like this (following answer https://stackoverflow.com/a/23497278):

formset = MyModelDetailsFormset(queryset=self.my_models, initial=[{'status':1},{'status':2}])

initial values don't kick-in. What I mean is that this:

for form in formset:
   print(form.fields['status'].initial)

prints None

Why is that happening? Am I missing something?

sebap123
  • 2,541
  • 6
  • 45
  • 81

1 Answers1

0

I see mistake I've made. According to documentation:

However, with model formsets, the initial values only apply to extra forms, those that aren’t attached to an existing model instance.

In other words there seems to be no way to initialize those values with initial dictionary.

sebap123
  • 2,541
  • 6
  • 45
  • 81