0

Have a look at this question fields and base_fields - Django. I want to do the exact stuff, except I need to set the initial for this field instead of choice. Even I have swap the order of lines also as mentioned in the answer of that question, but I am not getting the initial set value at frond end. mean while when I tried to check the code with pdb, I was getting no error and value also set to 5 successfully

class NewFlatpageForm(FlatpageForm):
        template_name = forms.ChoiceField(choices = [])
        def __init__(self, *args, **kwargs):
           super(NewFlatpageForm, self).__init__(*args, **kwargs)
           self.base_fields['template_name'].initial = 5
Community
  • 1
  • 1
NIlesh Sharma
  • 5,445
  • 6
  • 36
  • 53

1 Answers1

0

Make sure the super call goes first in your __init__ method, then modify the field's initial value in self.fields, not self.base_fields.

    def __init__(self, *args, **kwargs):
       super(NewFlatpageForm, self).__init__(*args, **kwargs)
       self.fields['template_name'].initial = 5
Alasdair
  • 298,606
  • 55
  • 578
  • 516