我有一个Django项目,它让我“FieldError:无法将关键字'游戏'解析到字段中。选择是:[选项列表]”。
有趣的是,它只会在settings.py中禁用DEBUG时发生。当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开启时,为什么它似乎正确加载。
感谢任何帮助。
UPDATE:跟踪,请求:
追踪(最近一次呼叫最后一次):
文件“/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行,在包装器中 return self.admin_site.admin_view(view)(* args,** kwargs)
文件“/home/bwareham/webapps/mprsb/lib/python2.7/django/utils/decorators.py”,第91行,_wrapped_view 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)
文件“/home/bwareham/webapps/mprsb/lib/python2.7/django/utils/decorators.py”,第25行,在_wrapper中 return bound_func(* args,** kwargs)
文件“/home/bwareham/webapps/mprsb/lib/python2.7/django/utils/decorators.py”,第91行,_wrapped_view response = view_func(request,* args,** kwargs)
文件“/home/bwareham/webapps/mprsb/lib/python2.7/django/utils/decorators.py”,第21行,在bound_func中 return func(self,* args2,** kwargs2)
文件“/home/bwareham/webapps/mprsb/lib/python2.7/django/db/transaction.py”,第224行,内部 return 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行,计数 return self.get_query_set()。count()
文件“/home/bwareham/webapps/mprsb/lib/python2.7/django/db/models/fields/related.py”,第567行,在get_query_set中 return 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行,在过滤器中 return self._filter_or_exclude(False,* args,** kwargs)
文件“/home/bwareham/webapps/mprsb/lib/python2.7/django/db/models/query.py”,第642行,在_filter_or_exclude中 clone.query.add_q(Q(* args,** kwargs))
文件“/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”%(名称,“,”。join(名称)))
FieldError:无法将关键字“游戏”解析为字段。选择是:名人堂,活跃,别名,battingchamps,bombat,船长,firstName,goldengloves,id,lastName,mostimproved,mvp,照片,新手,名册,性别,沃克,whippet
答案 0 :(得分:0)
这是某种加载问题。找到解决方案here。
能够在urls.py中的admin自动发现功能之前强制使用此代码段加载模型:
from django.db.models.loading import cache as model_cache
if not model_cache.loaded:
model_cache.get_models()
感谢所有试图帮助我排除故障的人。我总是学到一些东西。