Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
372 views
in Technique[技术] by (71.8m points)

python - FieldError: Cannot resolve keyword 'XXXX' into field

This is a very strange error. I only receive it on my heroku server.

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.

My production server is Heroku

Any ideas what could triggers the error?

UPDATE:

admin.py config is as follow:

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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:

# 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.

Reading through the comments in this old Django bug and this post by Chase Seibert considerably helped me in this process.

Although ticket#1796 appears to be fixed more than 6 years ago, it seems that some tiny issues still remain deep buried there.

Thats it! Thanks everyone.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...