I've got a Django project that throws me "FieldError: Cannot resolve keyword 'game' into field. Choices are: [list of choices]".
The funny thing is, it only occurs when DEBUG is disabled in settings.py. When DEBUG is enabled everything seems to work fine.
I have identified the code that triggers the error. My model looks like this:
from django.db import models
from django.forms import ModelForm
from time import strftime
from rostermaker.models import Player
from django.core.exceptions import ValidationError
from django.utils import timezone
class Game(models.Model):
when = models.DateTimeField(unique = True)
opponent = models.CharField(max_length = 50, default="TBD")
...
def __unicode__(self):
when = timezone.localtime(self.DateTime)
return when.strftime('%a, %b %d, %Y %I:%M %p')
class Stat(models.Model):
g = models.ForeignKey(Game, related_name = 'stat_game')
player = models.ForeignKey(Player, related_name = 'stat_player', limit_choices_to={'active': True})
...
In admin.py, the .count and .filter lines trigger the error:
def save_model(self, request, obj, form, change):
obj.save()
form.save_m2m()
count = obj.players.count()
women = obj.players.filter(sex='F')
women_count = women.count()
if count != 0:
women_pct = int((women_count/float(count))*100)
self.message_user(request,"Players scheduled: %s | Women: %s percent" % (count, women_pct))
else:
self.message_user(request,"Players scheduled: 0 | Women: 0 percent")
In a couple of views, lines similar to this one cause the error:
played_games_list = Game.objects.filter(DateTime__lte=timezone.now()).order_by('-when')
I'm using Django version 1.4 and PostgreSQL 8.4.20.
My best guess from what I've read here is that the Game class isn't loading properly. But why it appears to load properly when DEBUG is on mystifies me.
Any help is appreciated.
UPDATE: Traceback, as requested:
Traceback (most recent call last):
File "/home/bwareham/webapps/mprsb/lib/python2.7/django/core/handlers/base.py", line 111, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/home/bwareham/webapps/mprsb/lib/python2.7/django/contrib/admin/options.py", line 366, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "/home/bwareham/webapps/mprsb/lib/python2.7/django/utils/decorators.py", line 91, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/bwareham/webapps/mprsb/lib/python2.7/django/views/decorators/cache.py", line 89, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/home/bwareham/webapps/mprsb/lib/python2.7/django/contrib/admin/sites.py", line 196, in inner
return view(request, *args, **kwargs)
File "/home/bwareham/webapps/mprsb/lib/python2.7/django/utils/decorators.py", line 25, in _wrapper
return bound_func(*args, **kwargs)
File "/home/bwareham/webapps/mprsb/lib/python2.7/django/utils/decorators.py", line 91, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/bwareham/webapps/mprsb/lib/python2.7/django/utils/decorators.py", line 21, in bound_func
return func(self, *args2, **kwargs2)
File "/home/bwareham/webapps/mprsb/lib/python2.7/django/db/transaction.py", line 224, in inner
return func(*args, **kwargs)
File "/home/bwareham/webapps/mprsb/lib/python2.7/django/contrib/admin/options.py", line 955, in add_view
self.save_model(request, new_object, form, False)
File "/home/bwareham/webapps/mprsb/myproject/rostermaker/admin.py", line 69, in save_model
count = obj.players.count()
File "/home/bwareham/webapps/mprsb/lib/python2.7/django/db/models/manager.py", line 119, in count
return self.get_query_set().count()
File "/home/bwareham/webapps/mprsb/lib/python2.7/django/db/models/fields/related.py", line 567, in get_query_set
return super(ManyRelatedManager, self).get_query_set().using(db)._next_is_sticky().filter(**self.core_filters)
File "/home/bwareham/webapps/mprsb/lib/python2.7/django/db/models/query.py", line 624, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/home/bwareham/webapps/mprsb/lib/python2.7/django/db/models/query.py", line 642, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/home/bwareham/webapps/mprsb/lib/python2.7/django/db/models/sql/query.py", line 1250, in add_q
can_reuse=used_aliases, force_having=force_having)
File "/home/bwareham/webapps/mprsb/lib/python2.7/django/db/models/sql/query.py", line 1122, in add_filter
process_extras=process_extras)
File "/home/bwareham/webapps/mprsb/lib/python2.7/django/db/models/sql/query.py", line 1316, in setup_joins
"Choices are: %s" % (name, ", ".join(names)))
FieldError: Cannot resolve keyword 'game' into field. Choices are: Hall of Fame, active, alias, battingchamps, bombat, captains, firstName, goldengloves, id, lastName, mostimproved, mvp, photo, rookies, roster, sex, walker, whippet