Opened 6 years ago

Closed 6 years ago

#11247 closed (duplicate)

FieldError resulting from intermixing model forms with model definitions

Reported by: kmtracey Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Keywords:
Cc: girzel@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


With these model/form definitions:

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):
    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(

the contributors function does not work:

>>> 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/", line 6, in contributors
    return self.exclude(entry__isnull=True)
  File "/home/kmt/tmp/django/trunk/django/db/models/", line 141, in exclude
    return self.get_query_set().exclude(*args, **kwargs)
  File "/home/kmt/tmp/django/trunk/django/db/models/", line 473, in exclude
    return self._filter_or_exclude(True, *args, **kwargs)
  File "/home/kmt/tmp/django/trunk/django/db/models/", line 482, in _filter_or_exclude
    clone.query.add_q(~Q(*args, **kwargs))
  File "/home/kmt/tmp/django/trunk/django/db/models/sql/", line 1661, in add_q
    self.add_q(child, used_aliases)
  File "/home/kmt/tmp/django/trunk/django/db/models/sql/", line 1665, in add_q
  File "/home/kmt/tmp/django/trunk/django/db/models/sql/", line 1563, in add_filter
    negate=negate, process_extras=process_extras)
  File "/home/kmt/tmp/django/trunk/django/db/models/sql/", 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:

>>> 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:

Change History (3)

comment:1 Changed 6 years ago by taojian

  • Cc girzel@… added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 6 years ago by seveas

This looks an awful lot like #11448

comment:3 Changed 6 years ago by seveas

  • Resolution set to duplicate
  • Status changed from new to closed

It is in fact the same bug that causes this, closing this as a duplicate.

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