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
--
Ticket URL: <https://code.djangoproject.com/ticket/33399>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
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'] = <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:1>
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>
* version: 3.0 => 3.2
--
Ticket URL: <https://code.djangoproject.com/ticket/33399#comment:3>
* status: new => closed
* version: 3.2 => 3.0
* resolution: => invalid
Comment:
Thanks for the report, it was changed in
16a5a2a2c8d8dbf9cc3e033dd84b986bcaadb963, however it's
[https://docs.djangoproject.com/en/4.0/ref/models/fields/#django.db.models.Field.choices
documented] that:
> ''For **each model field that has `choices` set**, Django will add a
method to retrieve the human-readable name for the field’s current value.
See `get_FOO_display()` in the database API documentation.''
>
> ''Note that choices can be any sequence object – not necessarily a list
or tuple. This lets you construct choices dynamically. But if you find
yourself hacking choices to be dynamic, you’re probably better off using a
proper database table with a `ForeignKey`. `choices` **is meant for static
data that doesn’t change much, if ever.'**'
`choices=[]` means that it's set, if you want to get the previous behavior
without any validation you shouldn't define `choices` on a model field, or
set `choices=None`.
--
Ticket URL: <https://code.djangoproject.com/ticket/33399#comment:4>