2013-10-02 2 views
6

으로 해결할 수 없습니다. 이것은 매우 이상한 오류입니다. 나는 영웅 서버에서만 그것을받습니다.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 

내 프로덕션 서버에서 관리 사이트에서 뉴스 항목에 액세스하려고 할 때, 나는 (모든 것이 내 dev에 서버에서 잘 작동)이 오류를 얻을 : 여기

내 모델이 어떻게

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 

내 모델의 디자인은 사람의 테이블에 대한 외래 키를 필요로하지 않았다, 그래서 나는 간단한 CharField로 가기로 결정하고 대신 다운 메뉴 정기적 놓기를 사용했다. 그러나 Gunicorn은 get_country_names()의 일부로 Country 테이블이 News보다 먼저 호출 될 때 위에서 언급 한 오류를 발생시킵니다. get_country_names()를 삭제하고 Person 테이블의 country 필드를 일반 CharField로 변경하자 마자 문제가 해결되었습니다.

Chase Seibert의 this old Django bugthis post에 대한 의견을 읽는 것은이 과정에서 상당히 도움이되었습니다.

항공권 # 1796은 6 년 전에 고쳐졌지만 몇 가지 작은 쟁점이 여전히 깊게 묻혀있는 것으로 보입니다.

그게 전부입니다! 모두에게 감사드립니다.

2

단방향으로 작업하는 ManyToMany 관계가있었습니다. 나는 주변 환경을 어지럽히고 메인 애플리케이션의 이름을 몇 번 바꿨다. 라인을 따라 어딘가에, 난 INSTALLED_APPS 섹션에서 제거했다! 일단 다시 추가하면 작동합니다. 확실히 PEBKAC,하지만 이것은 언젠가 누군가를 도울 것입니다. 응용 프로그램이 대부분 작동했기 때문에 검사하는 것을 생각하는 데는 시간이 걸렸습니다.

는 예를 들어, 내 응용 프로그램은 deathvalleydogs라고합니다. 나는이 같은 여행이었다 Dogs을 나열하는 Trip에 대한 템플릿을 보여 주려고 할 때

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

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

: 나는 두 가지 모델이 있었다

: 나는의 오류를 가지고 다음
{% 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에 대한 템플릿을 보여줄 수 있었지만. tripsDog 개체의 m2m으로 만든 필드 여야합니다. 템플릿에서 해당 필드를 참조하지 않았지만 디버그 모드에서이 필드를 barfed했습니다.

나는 오류가 더 명시 적 있었다 좋겠지 만, 나는 마침내 내 실수를 발견 너무 행복 해요!

3

이 상황이 발생할 수있는 상황에 추가. 내 모델에서 찾을 수없는 필드를 검색했습니다.

코드에서 검색하면 해당 필드가 포함 된 쿼리 세트에 주석을 달고 그 쿼리 세트를 __in 검색으로 다른 복잡한 쿼리와 함께 제공하는 것으로 나타났습니다.

내 작품은 주변의 ID를 반환하고를 사용하는 그 주석의 검색어를 변경하는 것이 었습니다. 이 특별한 경우에 결과는 항상 작아서 ID 목록은 문제가되지 않았습니다.

관련 문제