This is a very strange error. I only receive it on my heroku server.
这是一个非常奇怪的错误。我只在我的heroku服务器上收到它。
Here is how my model is:
以下是我的模型:
# Abstract Model
class CommonInfo(models.Model):
active = models.BooleanField('Enabled?', default=False)
date_created = models.DateTimeField(auto_now_add=True)
date_updated = models.DateTimeField(auto_now=True)
class Meta:
abstract = True
class Country(CommonInfo):
name = models.CharField('Country Name', db_index=True, max_length=200, help_text='e.g. France')
official_name = models.CharField('Official Name', max_length=400, blank=True, help_text='e.g. French Republic')
population = models.IntegerField('Population', help_text='Population must be entered as numbers with no commas or separators, e.g. 39456123', null=True, blank=True)
alpha2 = models.CharField('ISO ALPHA-2 Code', max_length=2, blank=True)
class News(CommonInfo):
title = models.CharField('Title', max_length=250)
slug = models.CharField('slug', max_length=255, unique=True)
body = models.TextField('Body', null=True, blank=True)
excerpt = models.TextField('Excerpt', null=True, blank=True)
author = models.ForeignKey(Author)
country = models.ManyToManyField(Country, null=True, blank=True)
def __unicode__(self):
return self.title
When I try to access News items from Admin site on my production server, I get this error (everything works fine on my dev server):
当我尝试从我的生产服务器上的管理站点访问新闻项时,我收到此错误(我的开发服务器上的一切正常):
FieldError: Cannot resolve keyword 'news' into field. Choices are: active, alpha2, date_created, date_updated, id, name, official_name, population
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/query.py", line 687, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1271, in add_q
can_reuse=used_aliases, force_having=force_having)
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1139, in add_filter
process_extras=process_extras)
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1337, in setup_joins
"Choices are: %s" % (name, ", ".join(names)))
I run the same django (1.5.4) and python (2.7.2) versions on my production and development environments.
我在生产和开发环境中运行相同的django(1.5.4)和python(2.7.2)版本。
My production server is Heroku
我的生产服务器是Heroku
Any ideas what could triggers the error?
什么可能触发错误的想法?
UPDATE:
更新:
admin.py config is as follow:
admin.py配置如下:
from django.contrib import admin
from APP.models import Country, News
class NewsForm(ModelForm):
class Meta:
model = News
class NewsAdmin(ModelAdmin):
form = NewsForm
search_fields = ['title',
'country__name']
list_filter = ('country',
'active'
)
list_per_page = 30
list_editable = ('active', )
list_display = ('title',
'active'
)
list_select_related = True
prepopulated_fields = {"slug": ("title",)}
admin.site.register(Country)
admin.site.register(News, NewsAdmin)
3 个解决方案
#1
11
Finally, I was able to resolve the issue.
最后,我能够解决这个问题。
First, I managed to replicate the error in my local environment. At first, I was testing the application using built-in Django runserver. However, my production environment is Heroku that uses Gunicorn as webserver. When I switched to Gunicorn and foreman on my local server, I was able to replicate the error.
首先,我设法在我的本地环境中复制错误。起初,我使用内置的Django runserver测试应用程序。但是,我的生产环境是使用Gunicorn作为网络服务器的Heroku。当我在本地服务器上切换到Gunicorn和工头时,我能够复制错误。
Second, I tried to pin point the issue by going through the models and add/remove different components, fields. To explain the process better, I have to add a missing piece to the original question.
其次,我尝试通过遍历模型并添加/删除不同的组件,字段来确定问题。为了更好地解释这个过程,我必须在原始问题中添加一个缺失的部分。
The description I had posted above is kind of incomplete. I have another model in my models.py that I did not include in my original question, because I thought it was not relevant. Here is the complete model:
我上面发布的描述有点不完整。我的models.py中有另一个模型,我没有在原始问题中包含这个模型,因为我认为它不相关。这是完整的模型:
# Abstract Model
class CommonInfo(models.Model):
active = models.BooleanField('Enabled?', default=False)
date_created = models.DateTimeField(auto_now_add=True)
date_updated = models.DateTimeField(auto_now=True)
class Meta:
abstract = True
class Country(CommonInfo):
name = models.CharField('Country Name', db_index=True, max_length=200, help_text='e.g. France')
official_name = models.CharField('Official Name', max_length=400, blank=True, help_text='e.g. French Republic')
population = models.IntegerField('Population', help_text='Population must be entered as numbers with no commas or separators, e.g. 39456123', null=True, blank=True)
alpha2 = models.CharField('ISO ALPHA-2 Code', max_length=2, blank=True)
def get_country_names():
names = Country.objects.only('name').filter(active=1)
names = [(str(item), item) for item in names]
return names
class Person(CommonInfo):
name = models.CharField(max_length=200)
lastname = models.CharField(max_length=300)
country = models.CharField(max_length=250, choices=choices=get_country_names())
class News(CommonInfo):
title = models.CharField('Title', max_length=250)
slug = models.CharField('slug', max_length=255, unique=True)
body = models.TextField('Body', null=True, blank=True)
excerpt = models.TextField('Excerpt', null=True, blank=True)
author = models.ForeignKey(Author)
country = models.ManyToManyField(Country, null=True, blank=True)
def __unicode__(self):
return self.title
My model design didn't require a ForeignKey for Person's table, so I had decided to go with a simple CharField and instead, use a regular drop down menu. However, for some reason, Gunicorn raises the above mentioned error when, as part of the get_country_names(), the Country table is called before News. As soon as I deleted the get_country_names() and turned the country field on Person table into a regular CharField the issue was resolved.
我的模型设计不需要一个ForeignKey for Person的表,所以我决定使用一个简单的CharField,而是使用常规的下拉菜单。但是,出于某种原因,当作为get_country_names()的一部分,在新闻之前调用Country表时,Gunicorn会引发上述错误。一旦我删除了get_country_names()并将Person表上的country字段转换为常规CharField,问题就解决了。
Reading through the comments in this old Django bug and this post by Chase Seibert considerably helped me in this process.
通过这个旧的Django bug以及Chase Seibert发表的这篇文章阅读这个帖子对我来说非常有帮助。
Although ticket#1796 appears to be fixed more than 6 years ago, it seems that some tiny issues still remain deep buried there.
虽然机票#1796似乎在6年前修复,但似乎仍有一些小问题仍然深埋在那里。
Thats it! Thanks everyone.
而已!感谢大家。
#2
3
Adding to the possible situations under which this happens. I searched for the field that could not be found in any of my models.
增加发生这种情况的可能情况。我搜索了我的任何模型都找不到的字段。
Searching on the code I found that I was annotating a queryset with such field and then feeding that queryset as an __in
search to another (along other complex queries).
在搜索代码时,我发现我使用这样的字段对一个查询集进行注释,然后将该查询集作为__in搜索提供给另一个(沿着其他复杂的查询)。
My work around was to change that annotated queryset to return IDs and use that. On this particular case that result was always going to be small so the list of IDs was not a problem to pass.
我的工作是更改带注释的查询集以返回ID并使用它。在这种特殊情况下,结果总是很小,因此ID列表不是一个问题。
#3
2
I'd had some ManyToMany relationships that were working one-way. I had been messing around with my settings and changing the name of the main application a couple times. Somewhere along the lines, I had removed it from the INSTALLED_APPS
section! Once I added that back in, then it worked. Definitely PEBKAC, but maybe this will help someone some day. It took me a while to think of checking for that, since the app was mostly working.
我有一些单向工作的ManyToMany关系。我一直在乱搞我的设置并更改主应用程序的名称几次。在某些地方,我已经从INSTALLED_APPS部分删除了它!一旦我重新加入,那就有效了。绝对是PEBKAC,但也许有一天这会对某人有所帮助。我花了一段时间才考虑检查,因为应用程序主要是工作。
For example, my app is called deathvalleydogs
. I had two models:
例如,我的应用程序称为deathvalleydogs。我有两个型号:
class Trip(ModelBase):
dogs = models.ManyToManyField(Dog, related_name="trips")
class Dog(ModelBase):
name = models.CharField(max_length=200)
when I tried to show a template for a Trip
listing the Dogs
that were on the trip like this:
当我试图显示一个旅行模板,列出旅行中的狗,如下所示:
{% for dog in trip.dogs.all %}
<li><a href="/dogs/{{ dog.id }}">{{ dog.name }}</a></li>
{% endfor %}
then I got an error of:
然后我得到一个错误:
Cannot resolve keyword u'trips' into field. Choices are: active, birth_date, ...
Though I was still able to show a template for a Dog
listing the trips they were on. Notice that trips
should have been a field created by the m2m on the Dog
objects. I wasn't referencing that field in the template, but it barfed on that field anyway in debug mode.
虽然我仍然能够显示一个狗的模板列出他们所在的旅行。请注意,行程应该是由Dog对象上的m2m创建的字段。我没有在模板中引用该字段,但无论如何它在调试模式下都禁止该字段。
I wish the error had been more explicit, but I'm just so happy I finally found my mistake!!!
我希望错误更明确,但我很高兴我终于发现了我的错误!
#1
11
Finally, I was able to resolve the issue.
最后,我能够解决这个问题。
First, I managed to replicate the error in my local environment. At first, I was testing the application using built-in Django runserver. However, my production environment is Heroku that uses Gunicorn as webserver. When I switched to Gunicorn and foreman on my local server, I was able to replicate the error.
首先,我设法在我的本地环境中复制错误。起初,我使用内置的Django runserver测试应用程序。但是,我的生产环境是使用Gunicorn作为网络服务器的Heroku。当我在本地服务器上切换到Gunicorn和工头时,我能够复制错误。
Second, I tried to pin point the issue by going through the models and add/remove different components, fields. To explain the process better, I have to add a missing piece to the original question.
其次,我尝试通过遍历模型并添加/删除不同的组件,字段来确定问题。为了更好地解释这个过程,我必须在原始问题中添加一个缺失的部分。
The description I had posted above is kind of incomplete. I have another model in my models.py that I did not include in my original question, because I thought it was not relevant. Here is the complete model:
我上面发布的描述有点不完整。我的models.py中有另一个模型,我没有在原始问题中包含这个模型,因为我认为它不相关。这是完整的模型:
# Abstract Model
class CommonInfo(models.Model):
active = models.BooleanField('Enabled?', default=False)
date_created = models.DateTimeField(auto_now_add=True)
date_updated = models.DateTimeField(auto_now=True)
class Meta:
abstract = True
class Country(CommonInfo):
name = models.CharField('Country Name', db_index=True, max_length=200, help_text='e.g. France')
official_name = models.CharField('Official Name', max_length=400, blank=True, help_text='e.g. French Republic')
population = models.IntegerField('Population', help_text='Population must be entered as numbers with no commas or separators, e.g. 39456123', null=True, blank=True)
alpha2 = models.CharField('ISO ALPHA-2 Code', max_length=2, blank=True)
def get_country_names():
names = Country.objects.only('name').filter(active=1)
names = [(str(item), item) for item in names]
return names
class Person(CommonInfo):
name = models.CharField(max_length=200)
lastname = models.CharField(max_length=300)
country = models.CharField(max_length=250, choices=choices=get_country_names())
class News(CommonInfo):
title = models.CharField('Title', max_length=250)
slug = models.CharField('slug', max_length=255, unique=True)
body = models.TextField('Body', null=True, blank=True)
excerpt = models.TextField('Excerpt', null=True, blank=True)
author = models.ForeignKey(Author)
country = models.ManyToManyField(Country, null=True, blank=True)
def __unicode__(self):
return self.title
My model design didn't require a ForeignKey for Person's table, so I had decided to go with a simple CharField and instead, use a regular drop down menu. However, for some reason, Gunicorn raises the above mentioned error when, as part of the get_country_names(), the Country table is called before News. As soon as I deleted the get_country_names() and turned the country field on Person table into a regular CharField the issue was resolved.
我的模型设计不需要一个ForeignKey for Person的表,所以我决定使用一个简单的CharField,而是使用常规的下拉菜单。但是,出于某种原因,当作为get_country_names()的一部分,在新闻之前调用Country表时,Gunicorn会引发上述错误。一旦我删除了get_country_names()并将Person表上的country字段转换为常规CharField,问题就解决了。
Reading through the comments in this old Django bug and this post by Chase Seibert considerably helped me in this process.
通过这个旧的Django bug以及Chase Seibert发表的这篇文章阅读这个帖子对我来说非常有帮助。
Although ticket#1796 appears to be fixed more than 6 years ago, it seems that some tiny issues still remain deep buried there.
虽然机票#1796似乎在6年前修复,但似乎仍有一些小问题仍然深埋在那里。
Thats it! Thanks everyone.
而已!感谢大家。
#2
3
Adding to the possible situations under which this happens. I searched for the field that could not be found in any of my models.
增加发生这种情况的可能情况。我搜索了我的任何模型都找不到的字段。
Searching on the code I found that I was annotating a queryset with such field and then feeding that queryset as an __in
search to another (along other complex queries).
在搜索代码时,我发现我使用这样的字段对一个查询集进行注释,然后将该查询集作为__in搜索提供给另一个(沿着其他复杂的查询)。
My work around was to change that annotated queryset to return IDs and use that. On this particular case that result was always going to be small so the list of IDs was not a problem to pass.
我的工作是更改带注释的查询集以返回ID并使用它。在这种特殊情况下,结果总是很小,因此ID列表不是一个问题。
#3
2
I'd had some ManyToMany relationships that were working one-way. I had been messing around with my settings and changing the name of the main application a couple times. Somewhere along the lines, I had removed it from the INSTALLED_APPS
section! Once I added that back in, then it worked. Definitely PEBKAC, but maybe this will help someone some day. It took me a while to think of checking for that, since the app was mostly working.
我有一些单向工作的ManyToMany关系。我一直在乱搞我的设置并更改主应用程序的名称几次。在某些地方,我已经从INSTALLED_APPS部分删除了它!一旦我重新加入,那就有效了。绝对是PEBKAC,但也许有一天这会对某人有所帮助。我花了一段时间才考虑检查,因为应用程序主要是工作。
For example, my app is called deathvalleydogs
. I had two models:
例如,我的应用程序称为deathvalleydogs。我有两个型号:
class Trip(ModelBase):
dogs = models.ManyToManyField(Dog, related_name="trips")
class Dog(ModelBase):
name = models.CharField(max_length=200)
when I tried to show a template for a Trip
listing the Dogs
that were on the trip like this:
当我试图显示一个旅行模板,列出旅行中的狗,如下所示:
{% for dog in trip.dogs.all %}
<li><a href="/dogs/{{ dog.id }}">{{ dog.name }}</a></li>
{% endfor %}
then I got an error of:
然后我得到一个错误:
Cannot resolve keyword u'trips' into field. Choices are: active, birth_date, ...
Though I was still able to show a template for a Dog
listing the trips they were on. Notice that trips
should have been a field created by the m2m on the Dog
objects. I wasn't referencing that field in the template, but it barfed on that field anyway in debug mode.
虽然我仍然能够显示一个狗的模板列出他们所在的旅行。请注意,行程应该是由Dog对象上的m2m创建的字段。我没有在模板中引用该字段,但无论如何它在调试模式下都禁止该字段。
I wish the error had been more explicit, but I'm just so happy I finally found my mistake!!!
我希望错误更明确,但我很高兴我终于发现了我的错误!