﻿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
11247	FieldError resulting from intermixing model forms with model definitions	Karen Tracey	nobody	"With these model/form definitions:

{{{
#!python
from django.db import models
from django import forms

class AManager(models.Manager):
    def contributors(self):
        return self.exclude(entry__isnull=True)

class Author(models.Model):
    name = models.CharField(max_length=44)
    is_translator = models.BooleanField(""Translator"", default=False)    
    def __unicode__(self):
        return self.name
    objects = AManager()

class Sample(models.Model):
    translator = models.ForeignKey(Author,blank=True,limit_choices_to={'is_translator':True})
    #translator = models.ForeignKey(Author,blank=True)

class SampleForm(forms.ModelForm):
    class Meta:
        model = Sample
        
class Entry(models.Model):
    value = models.CharField(max_length=23)
    author = models.ForeignKey(Author)
    def __unicode__(self):
        return u'%s (author: %s)' % (self.value, unicode(self.author))
}}}

the `contributors` function does not work:

{{{
#!python
>>> from ttt.models import Author
>>> Author.objects.contributors()
Traceback (most recent call last):
  File ""<console>"", line 1, in <module>
  File ""/home/kmt/software/web/playground/ttt/models.py"", line 6, in contributors
    return self.exclude(entry__isnull=True)
  File ""/home/kmt/tmp/django/trunk/django/db/models/manager.py"", line 141, in exclude
    return self.get_query_set().exclude(*args, **kwargs)
  File ""/home/kmt/tmp/django/trunk/django/db/models/query.py"", line 473, in exclude
    return self._filter_or_exclude(True, *args, **kwargs)
  File ""/home/kmt/tmp/django/trunk/django/db/models/query.py"", line 482, in _filter_or_exclude
    clone.query.add_q(~Q(*args, **kwargs))
  File ""/home/kmt/tmp/django/trunk/django/db/models/sql/query.py"", line 1661, in add_q
    self.add_q(child, used_aliases)
  File ""/home/kmt/tmp/django/trunk/django/db/models/sql/query.py"", line 1665, in add_q
    can_reuse=used_aliases)
  File ""/home/kmt/tmp/django/trunk/django/db/models/sql/query.py"", line 1563, in add_filter
    negate=negate, process_extras=process_extras)
  File ""/home/kmt/tmp/django/trunk/django/db/models/sql/query.py"", line 1727, in setup_joins
    ""Choices are: %s"" % (name, "", "".join(names)))
FieldError: Cannot resolve keyword 'entry' into field. Choices are: id, is_translator, name, sample
>>> 

}}}

Moving the !SampleForm definition to below the Entry model definition allow the `contributors` function to work:

{{{
#!python
>>> from ttt.models import Author
>>> Author.objects.contributors()
[]
>>> 
}}}

Alternatively, removing the `limit_choices_to` (swap which of the lines is commented) on the Sample model's !ForeignKey to Author avoids the error.  This problem feels a bit like #10405 but I'm not sure it's an exact dupe so I've put it in its own ticket.  Discovered investigating this report on django-users: http://groups.google.com/group/django-users/browse_thread/thread/c05731f3392f1174#"		closed	Database layer (models, ORM)	dev		duplicate		girzel@…	Unreviewed	0	0	0	0	0	0
