#33399: Overriding Choices for a field in the Form not working when using 
ModelForm
| Django 3.2
-------------------------------+--------------------------------------
     Reporter:  Swaroop P      |                    Owner:  nobody
         Type:  Uncategorized  |                   Status:  new
    Component:  Forms          |                  Version:  3.0
     Severity:  Normal         |               Resolution:
     Keywords:  modelform      |             Triage Stage:  Unreviewed
    Has patch:  0              |      Needs documentation:  0
  Needs tests:  0              |  Patch needs improvement:  0
Easy pickings:  0              |                    UI/UX:  0
-------------------------------+--------------------------------------
Description changed by Swaroop P:

Old description:

> Hi All
>
> Facing the following issue after migrating to 3.2.
>
> Example:
> {{{
>
> class SomeModel(models.Model):
>        field1 = models.CharField(choices=[])
>
> class SomeModelForm(forms.ModelForm):
>
>      __init__(*args, **kwargs):
>          super().__init__(*args **kwargs)
>          self.fields['field1'] = <some choices dynamically generated>
>
>     class Meta:
>          model = SomeModel
>          fields = [ 'field1' ]
> }}}
>

> In the above case any choice selected is shown with the error "not a
> valid choice" this is happening because model fields is revalidated again
> in modelform._post_clean() function.
>
> Intermediate solution is to remove choices from modelfield and
> reinitalize field in form __init__ wiht dynamic choices. But In previous
> versions We had choices to only override some attributes of a field
> rather than reinitalizing whole field.
>
> Thanks

New description:

 Hi All

 Facing the following issue after migrating to 3.2.

 Example:
 {{{

 class SomeModel(models.Model):
        field1 = models.CharField(choices=[])

 class SomeModelForm(forms.ModelForm):

      __init__(*args, **kwargs):
          super().__init__(*args **kwargs)
          self.fields['field1'].choices = <some choices dynamically
 generated>

     class Meta:
          model = SomeModel
          fields = [ 'field1' ]
 }}}


 In the above case any choice selected is shown with the error "not a valid
 choice" this is happening because model fields is revalidated again  in
 modelform._post_clean() function.

 Intermediate solution is to remove choices from modelfield and reinitalize
 field in form __init__ wiht dynamic choices. But In previous versions We
 had choices to only override some attributes of a field rather than
 reinitalizing whole field.

 Thanks

--

-- 
Ticket URL: <https://code.djangoproject.com/ticket/33399#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/069.815cfa17ee41eac9994bbb4d54298b3c%40djangoproject.com.

Reply via email to