2013-10-02 63 views
6

這是一個非常奇怪的錯誤。我只在我的heroku服務器上收到它。FieldError:無法將關鍵字'XXXX'解析爲字段

這裏是我的模型是如何:

# 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 

當我嘗試訪問我的生產服務器上的管理網站的新聞,我得到這個錯誤(一切正常,我的開發服務器上):

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))) 

我在我的生產和開發環境中運行相同的django(1.5.4)和python(2.7.2)版本。

我的產品服務器是Heroku

任何想法什麼會觸發錯誤?

UPDATE

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) 
+0

所以這發生在你訪問管理員?你能發佈你的admin.py文件代碼嗎? – jproffitt

+0

無論我需要訪問ManyToMany關係。 – AlirezaJ

+0

你可以發佈產生錯誤的相關代碼嗎? – jproffitt

回答

10

最後,我能解決這個問題。

首先,我設法在我的本地環境中複製錯誤。起初,我正在使用內置的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 

我的模型設計並沒有要求對人的表一個ForeignKey,所以我決定去用一個簡單的CharField,而是,使用常規的下拉菜單。但是,由於某種原因,當作爲get_country_names()的一部分時,Gunicorn引發了上述錯誤,在News之前調用Country表。一旦我刪除了get_country_names(),並將Person表中的country字段變爲常規CharField,問題就解決了。

通過Chase Seibert在this old Django bugthis post中的註釋閱讀,在這個過程中幫助很大。

儘管門票#1796在6年前似乎是固定的,但似乎仍然有一些小小的問題深藏在那裏。

那就是它!感謝大家。

2

我有一些單向工作的ManyToMany關係。我一直在搞亂我的設置,並改變了主應用程序的名稱幾次。沿線的某處,我已將它從INSTALLED_APPS部分刪除!一旦我添加回來,那麼它就起作用了。肯定是PEBKAC,但也許這有助於某些人。我花了一段時間纔想到檢查,因爲該應用程序主要是工作。

例如,我的應用叫做deathvalleydogs。我有兩個型號:

class Trip(ModelBase): 
    dogs = models.ManyToManyField(Dog, related_name="trips") 

class Dog(ModelBase): 
    name = models.CharField(max_length=200) 

,當我試圖表明一個Trip一個模板,列出了在這樣的行程Dogs

{% for dog in trip.dogs.all %} 
    <li><a href="/dogs/{{ dog.id }}">{{ dog.name }}</a></li> 
{% endfor %} 

然後我得到了一個錯誤:

Cannot resolve keyword u'trips' into field. Choices are: active, birth_date, ... 

雖然我仍然能夠顯示Dog的模板,列出了他們所在的行程。請注意,trips應該是由m2m在Dog對象上創建的字段。我沒有在模板中引用該字段,但它仍然在調試模式下在該字段上被禁止。

我希望錯誤更明確,但我很高興終於發現我的錯誤!

3

添加到發生這種情況的可能情況。我搜索了在我的任何模型中找不到的字段。

在代碼上搜索我發現我用這樣的字段註釋了一個查詢集,然後將該查詢集作爲__in搜索提供給另一個(沿着其他複雜查詢)。

我的工作是改變註釋的查詢集返回ID並使用它。在這個特殊情況下,結果總是很小,因此ID列表並不是一個問題。

相關問題