Opened 16 years ago

Closed 15 years ago

#11247 closed (duplicate)

FieldError resulting from intermixing model forms with model definitions

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

Description

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

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

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

Change History (3)

comment:1 by Eric Abrahamsen, 16 years ago

Cc: girzel@… added

comment:2 by Dennis Kaarsemaker, 15 years ago

This looks an awful lot like #11448

comment:3 by Dennis Kaarsemaker, 15 years ago

Resolution: duplicate
Status: newclosed

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