﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
22000	'ModelChoiceField' object has no attribute 'to_field_name'	anonymous	nobody	"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... )

"	Bug	closed	Forms	1.6	Normal	invalid			Unreviewed	0	0	0	0	0	0
