#30014: Initialising disabled ModelChoiceField yields 'Select a valid choice'-error despite initialised option being valid -------------------------------------+------------------------------------- Reporter: thoha | Owner: nobody Type: Bug | Status: new Component: Forms | Version: 2.1 Severity: Normal | Resolution: Keywords: forms, disabled | Triage Stage: field, error, to_field_name | Unreviewed Has patch: 0 | Needs documentation: 0 Needs tests: 0 | Patch needs improvement: 0 Easy pickings: 0 | UI/UX: 0 -------------------------------------+-------------------------------------
Comment (by Etienne Chove): I really disagree just adding notice in the doc. In this case, when we want inial from a blanked instance field, we should add many lines each time to test if ther's a **pk** or if it is **None**: {{{ class MyForm(Form): if the_object.the_filed is None: initial = None else: initial = the_object.the_filed.pk # or initial = the_object.the_filed and the_object.the_filed.pk or None field = ModelChoiceField( queryset=ChoiceOptionModel.objects.all(), disabled=False, initial=initial) }}} Allowing initial as a model instance is a really good idea, it reduces 4 lines each time. Another patch, if we do not want to use model object everywhere, would be to evaluate type in the **__init__** method of the **ModelChoiceField** so we just store **pk**. -- Ticket URL: <https://code.djangoproject.com/ticket/30014#comment:12> 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 django-updates+unsubscr...@googlegroups.com. To post to this group, send email to django-updates@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/063.cec2a3f52fb9f403f51c6e5806fc75fe%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.