[Django] #22000: 'ModelChoiceField' object has no attribute 'to_field_name'

27 views
Skip to first unread message

Django

unread,
Feb 10, 2014, 9:39:13 AM2/10/14
to django-...@googlegroups.com
#22000: 'ModelChoiceField' object has no attribute 'to_field_name'
----------------------------+--------------------
Reporter: anonymous | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: 1.6
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+--------------------
Hello,

I am getting the error, [ 'ModelChoiceField' object has no attribute
'to_field_name' ],
when I try to use the following model (from an open source library) in the
ModelChoiceField with python 2.7.3

This is the model in question.
https://sourcegraph.com/github.com/celery/django-
celery/symbols/python/djcelery/models/IntervalSchedule#examples

This is the code that causes the error in a form. (The original code was a
form that inherited from the ModelChoiceField, but ModelChoiceField itself
also gives the error. )
{{{#!python
widgets={'interval': ModelChoiceField(
queryset=IntervalSchedule.objects.all(), empty_label=" :-( ")}
}}}
The modelform, based on PeriodicTask in which the widget is changed, is
called by modelformset_factory.
(from djcelery.models import PeriodicTask, IntervalSchedule )

/usr/local/lib/python2.7/dist-packages/django/forms/models.py in
prepare_value

{{{#!python
def prepare_value(self, value):
if hasattr(value, '_meta'):
if self.to_field_name:
return value.serializable_value(self.to_field_name)
else:
return value.pk
return super(ModelChoiceField, self).prepare_value(value)
}}}

Django Version: 1.6
Exception Type: AttributeError
Exception Value:
'ModelChoiceField' object has no attribute 'to_field_name'
Exception Location: /usr/local/lib/python2.7/dist-
packages/django/forms/models.py in prepare_value, line 1115
Python Executable: /usr/bin/python
Python Version: 2.7.3

I do see the to_field_name in the code on
https://github.com/django/django/blob/stable/1.6.x/django/forms/models.py
but it is initialized on init.

So could this be race condition bug?
(Since I don't get the error with the roughly same code if the widget is
not changed. )

(Am not clicking easy pickings due to the possibility of it being a race
bug. Getting the to_field_name out of the init and into the class would be
easy enough to fix this bug, but if something else is going on... )

--
Ticket URL: <https://code.djangoproject.com/ticket/22000>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 11, 2014, 4:02:41 AM2/11/14
to django-...@googlegroups.com
#22000: 'ModelChoiceField' object has no attribute 'to_field_name'
---------------------------+--------------------------------------

Reporter: anonymous | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: 1.6
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------+--------------------------------------
Changes (by anonymous):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

Changing the form directly works, but not using a widget.
interval = ModelChoiceField(
queryset=IntervalSchedule.objects.all(),initial=PeriodicTask.interval,
empty_label="...", required=False)

--
Ticket URL: <https://code.djangoproject.com/ticket/22000#comment:1>

Django

unread,
Feb 11, 2014, 12:28:38 PM2/11/14
to django-...@googlegroups.com
#22000: 'ModelChoiceField' object has no attribute 'to_field_name'
---------------------------+--------------------------------------
Reporter: anonymous | Owner: nobody
Type: Bug | Status: closed
Component: Forms | Version: 1.6
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------+--------------------------------------
Changes (by Alasdair):

* status: new => closed
* resolution: => invalid


Comment:

A model choice field is not a widget, it is not correct to use it in
`widgets`.

I'm closing this ticket, because it appears to be an issue with your code,
not Django. If you need further help customising the form in your code,
please see wiki:UseSupportChannels

--
Ticket URL: <https://code.djangoproject.com/ticket/22000#comment:2>

Django

unread,
Feb 11, 2014, 12:31:21 PM2/11/14
to django-...@googlegroups.com
#22000: 'ModelChoiceField' object has no attribute 'to_field_name'
---------------------------+--------------------------------------
Reporter: anonymous | Owner: nobody
Type: Bug | Status: closed
Component: Forms | Version: 1.6
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------+--------------------------------------

Comment (by Alasdair):

Sorry, I got the link wrong in the previous comment. I meant to link to
TicketClosingReasons/UseSupportChannels

--
Ticket URL: <https://code.djangoproject.com/ticket/22000#comment:3>

Reply all
Reply to author
Forward
0 new messages