#20202: BaseModelForm uses model_to_dict to provide initial, causes problems for ModelChoiceField with non-pk to_field_name -------------------------------+------------------------------------ Reporter: valtron2000@… | Owner: nobody Type: Bug | Status: new Component: Forms | Version: 1.5 Severity: Normal | Resolution: Keywords: | Triage Stage: Accepted Has patch: 0 | Needs documentation: 0 Needs tests: 0 | Patch needs improvement: 0 Easy pickings: 0 | UI/UX: 0 -------------------------------+------------------------------------
Comment (by bmispelon): I've been digging a bit and now I'm not sure if this is technically a bug or not. The main problem is that since the `to_field_name` attribute is undocumented, what it's supposed to do is not exactly clear. The fact is that there is a (passing) test the issue you describe: https://github.com/django/django/blob/master/tests/model_forms/tests.py#L1508-L1513 Without documentation, a test is probably the next best thing to know the intended behavior. From the test, it looks like the form field's `to_field_name` should match the model field's `to_field`, which in your example is not the case (and causes the issue). Note that the test models is quite similar to your example (the difference being the existence of the `to_field` attribute): https://github.com/django/django/blob/master/tests/model_forms/models.py#L174 So I'm leaving this as "Accepted", but I think the first thing to do would be to document `ModelChoiceField.to_field_name` and then make sure the implementation matches the documentation. -- Ticket URL: <https://code.djangoproject.com/ticket/20202#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 django-updates+unsubscr...@googlegroups.com. To post to this group, send email to django-updates@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.