我有一个 Django 项目,它向我抛出“FieldError:无法将关键字'游戏'解析到字段中。选择是:[选择列表]”。
有趣的是,它仅在 settings.py 中禁用 DEBUG 时发生。启用调试后,一切似乎都正常。
我已经确定了触发错误的代码。我的模型如下所示:
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})
...
在 admin.py 中,.count 和 .filter 行会触发错误:
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")
在几个视图中,与此类似的行会导致错误:
played_games_list = Game.objects.filter(DateTime__lte=timezone.now()).order_by('-when')
我正在使用 Django 1.4 版和 PostgreSQL 8.4.20。
从我在这里读到的内容中,我最好的猜测是 Game 类没有正确加载。但是为什么当 DEBUG 打开时它似乎可以正确加载让我感到困惑。
任何帮助表示赞赏。
更新:根据要求进行追溯:
回溯(最近一次通话最后):
文件“/home/bwareham/webapps/mprsb/lib/python2.7/django/core/handlers/base.py”,第 111 行,在 get_response response = callback(request, *callback_args, **callback_kwargs)
文件“/home/bwareham/webapps/mprsb/lib/python2.7/django/contrib/admin/options.py”,第 366 行,在包装器中返回 self.admin_site.admin_view(view)(*args, **kwargs)
_wrapped_view 中的文件“/home/bwareham/webapps/mprsb/lib/python2.7/django/utils/decorators.py”,第 91 行 response = view_func(request, *args, **kwargs)
文件“/home/bwareham/webapps/mprsb/lib/python2.7/django/views/decorators/cache.py”,第 89 行,在 _wrapped_view_func response = view_func(request, *args, **kwargs)
文件“/home/bwareham/webapps/mprsb/lib/python2.7/django/contrib/admin/sites.py”,第 196 行,内部返回视图(请求,*args,**kwargs)
_wrapper 中的文件“/home/bwareham/webapps/mprsb/lib/python2.7/django/utils/decorators.py”,第 25 行 return bound_func(*args, **kwargs)
_wrapped_view 中的文件“/home/bwareham/webapps/mprsb/lib/python2.7/django/utils/decorators.py”,第 91 行 response = view_func(request, *args, **kwargs)
文件“/home/bwareham/webapps/mprsb/lib/python2.7/django/utils/decorators.py”,第 21 行,在 bound_func 返回 func(self, *args2, **kwargs2)
文件“/home/bwareham/webapps/mprsb/lib/python2.7/django/db/transaction.py”,第 224 行,内部返回 func(*args, **kwargs)
文件“/home/bwareham/webapps/mprsb/lib/python2.7/django/contrib/admin/options.py”,第 955 行,在 add_view self.save_model(request, new_object, form, False)
文件“/home/bwareham/webapps/mprsb/myproject/rostermaker/admin.py”,第 69 行,在 save_model count = obj.players.count()
文件“/home/bwareham/webapps/mprsb/lib/python2.7/django/db/models/manager.py”,第 119 行,计数返回 self.get_query_set().count()
文件“/home/bwareham/webapps/mprsb/lib/python2.7/django/db/models/fields/related.py”,第 567 行,在 get_query_set 返回 super(ManyRelatedManager, self).get_query_set().using(db )._next_is_sticky().filter(**self.core_filters)
文件“/home/bwareham/webapps/mprsb/lib/python2.7/django/db/models/query.py”,第 624 行,过滤器返回 self._filter_or_exclude(False, *args, **kwargs)
_filter_or_exclude clone.query.add_q(Q(*args, **kwargs)) 中的文件“/home/bwareham/webapps/mprsb/lib/python2.7/django/db/models/query.py”,第 642 行
文件“/home/bwareham/webapps/mprsb/lib/python2.7/django/db/models/sql/query.py”,第 1250 行,在 add_q can_reuse=used_aliases, force_having=force_having)
文件“/home/bwareham/webapps/mprsb/lib/python2.7/django/db/models/sql/query.py”,第 1122 行,在 add_filter process_extras=process_extras)
文件“/home/bwareham/webapps/mprsb/lib/python2.7/django/db/models/sql/query.py”,第 1316 行,在 setup_joins “选择是:%s”%(名称,“,”。加入(姓名)))
FieldError:无法将关键字“游戏”解析为字段。选项有:名人堂、活跃、别名、battingchamps、bombat、船长、名字、goldengloves、id、lastName、mostimproved、mvp、照片、新秀、花名册、性别、步行者、whippet