Opened 5 years ago

Closed 5 years ago

#31043 closed Bug (duplicate)

Form with ModelChoiceField and disabled failed when the initial is model instance.

Reported by: Eran Keydar Owned by: nobody
Component: Forms Version: 3.0
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Got this issue in some more complex example, narrows down to this scenario:

class Form1(forms.Form):
    user = forms.ModelChoiceField(
        queryset=get_user_model().objects.all(),
        disabled=True,
        initial=get_user_model().objects.get(pk=1)
    )
    label = forms.CharField()

Then

In [12]: f = Form1(data={'label':'aaa'})                                        

In [13]: f.is_valid()                                                           
Out[13]: False

In [14]: f.errors                                                               
Out[14]: {'user': ['Select a valid choice. That choice is not one of the available choices.']}

If the initial changed to the pk itself, everything works fine

Change History (1)

comment:1 by Mariusz Felisiak, 5 years ago

Component: UncategorizedForms
Resolution: duplicate
Status: newclosed
Summary: From with ModelChoiceField and disabled failed when the initial is model instance and not pkForm with ModelChoiceField and disabled failed when the initial is model instance.
Type: UncategorizedBug
Version: 2.23.0

Duplicate of #30014. This is fixed in Django 3.0.

Note: See TracTickets for help on using tickets.
Back to Top