Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#22000 closed Bug (invalid)

'ModelChoiceField' object has no attribute 'to_field_name'

Reported by: anonymous Owned by: nobody
Component: Forms Version: 1.6
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no



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.

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. )

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/ in prepare_value

  def prepare_value(self, value):
      if hasattr(value, '_meta'):
          if self.to_field_name:
              return value.serializable_value(self.to_field_name)
      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/ 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 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... )

Change History (3)

comment:1 by anonymous, 10 years ago

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

comment:2 by Alasdair, 10 years ago

Resolution: invalid
Status: newclosed

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

comment:3 by Alasdair, 10 years ago

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

Note: See TracTickets for help on using tickets.
Back to Top