#28316: ModelChoiceField with to_field_name fails to render a widget properly ------------------------------------------+------------------------ Reporter: László Károlyi | Owner: nobody Type: Bug | Status: new Component: Forms | Version: master Severity: Normal | Keywords: Triage Stage: Unreviewed | Has patch: 0 Needs documentation: 0 | Needs tests: 0 Patch needs improvement: 0 | Easy pickings: 0 UI/UX: 0 | ------------------------------------------+------------------------ Hey guys,
I have two models something along the following: {{{#!python class M1(Model): code = CharField() name = CharField() class M2(Model): m1 = ForeignKey(to=M1) }}} I have a custom modelform, something along: {{{#!python class MyForm(ModelForm): m1 = ModelChoiceField(to_field_name='code') class Meta(object): model = M2 exclude = () }}} When I try to render this form with an instance of `M2`, because of `to_field_name` not being `pk` of `m1`, the form will render without the option pointing to `M1` selected. I managed to track the problem back to `BoundField.value()`, which calls `self.field.prepare_value(data)` with the `PK` passed to the instance of the `ModelChoiceField`. There, `ModelChoiceField.prepare_value()` should be able to know how to resolve the passed `PK` to the parameter in `to_field_name`, but it doesn't do it. Because of that, I'm forced not to pass the `to_field_name` parameter, and let the select widget render itself with `PK`s instead of the wished `code` fields. Please look into this, should be fairly easy to reproduce. As I could see from googling, [https://stackoverflow.com/questions/9827057/django- modelchoicefield-use-something-other-than-id#comment21720211_11522820 this has formerly already been a problem], and now it's present again. -- Ticket URL: <https://code.djangoproject.com/ticket/28316> 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/050.aa387c5e88857548ea79b04cb4c1d67f%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.