这是一个非常奇怪的错误.我只在我的heroku服务器上收到它.
以下是我的模型:
# 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
Run Code Online (Sandbox Code Playgroud)
当我尝试从我的生产服务器上的管理站点访问新闻项时,我收到此错误(我的开发服务器上的一切正常):
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)))
Run Code Online (Sandbox Code Playgroud)
我在生产和开发环境中运行相同的django(1.5.4)和python(2.7.2)版本.
我的生产服务器是Heroku
什么可能触发错误的想法?
更新:
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)
Run Code Online (Sandbox Code Playgroud)
Ali*_*zaJ 12
最后,我能够解决这个问题.
首先,我设法在我的本地环境中复制错误.起初,我使用内置的Django runserver测试应用程序.但是,我的生产环境是使用Gunicorn作为网络服务器的Heroku.当我在本地服务器上切换到Gunicorn和工头时,我能够复制错误.
其次,我尝试通过遍历模型并添加/删除不同的组件,字段来指出问题.为了更好地解释这个过程,我必须在原始问题中添加一个缺失的部分.
我上面发布的描述有点不完整.我的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
Run Code Online (Sandbox Code Playgroud)
我的模型设计不需要一个ForeignKey for Person的表,所以我决定使用一个简单的CharField,而是使用常规的下拉菜单.但是,出于某种原因,当作为get_country_names()的一部分,在新闻之前调用Country表时,Gunicorn会引发上述错误.一旦我删除了get_country_names()并将Person表上的country字段转换为常规CharField,问题就解决了.
阅读这个旧的Django bug和Chase Seibert的这篇文章中的评论,在这个过程中帮助了我.
虽然机票#1796似乎是在6年前修复的,但似乎仍有一些小问题仍然深埋在那里.
而已!感谢大家.
增加了发生这种情况的可能情况。我搜索了在我的任何模型中都找不到的字段。
搜索代码后,我发现我正在用这样的字段注释查询集,然后将该__in查询集作为对另一个查询的搜索(以及其他复杂查询)。
我的解决方法是更改带注释的查询集以返回ID并使用它。在这种特殊情况下,结果总是很小,因此ID列表不会成为问题。
| 归档时间: |
|
| 查看次数: |
30702 次 |
| 最近记录: |