#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 -------------------------------+------------------------------------ Changes (by bmispelon):
* cc: bmispelon@… (added) * needs_docs: => 0 * needs_better_patch: => 0 * needs_tests: => 0 * stage: Unreviewed => Accepted Comment: This definitely looks like a bug but it might be a bit tricky to fix (it will at least require some changes to `model_to_dict`'s signature). As noted, the issue is that `model_to_dict` will always return the primary key of the model, not taking into account the field's `to_field_name` attribute. The problem is that `model_to_dict` does not have access to the form's fields: it only gets passed the instance, a list of strings corresponding to the model's fields present in the form, and a list of strings of the fields to exclude. Therefore, it has no way to access the `to_field_name` attribute and adjust the value accordingly. On a side-note, it seems that `ModelChoiceField.to_field_name` is completely undocumented, though there seem to be a few tests for it. This ticket could be a good excuse to finally document it. -- Ticket URL: <https://code.djangoproject.com/ticket/20202#comment:1> 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.