FieldError: cannot delete keyword 'XXXX' in field

This is a very strange mistake. I get it only on my server to the hero.

This 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='eg France') official_name = models.CharField('Official Name', max_length=400, blank=True, help_text='eg French Republic') population = models.IntegerField('Population', help_text='Population must be entered as numbers with no commas or separators, eg 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 the 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 am running the same versions of django (1.5.4) and python (2.7.2) in my development and development environment.

My Heroku Server

Any ideas that might cause a bug?

UPDATE

The admin.py configuration is as follows:

 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) 
+10
source share
5 answers

Finally, I was able to solve the problem.

Firstly, I was able to reproduce the error in the local environment. At first I tested the application using the built-in Django server. However, my production environment is Heroku, which uses Gunicorn as a web server. When I switched to Gunicorn and the foreman on my local server, I was able to reproduce the error.

Secondly, I tried to indicate the problem by looking at the models and adding / removing different components, fields. To better explain the process, I must add the missing part to the original question.

The description I wrote above is incomplete. I have one more model in my .py models that I did not include in my original question because I thought it didn't matter. 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='eg France') official_name = models.CharField('Official Name', max_length=400, blank=True, help_text='eg French Republic') population = models.IntegerField('Population', help_text='Population must be entered as numbers with no commas or separators, eg 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 did not require a ForeignKey for Person table, so I decided to go with a simple CharField and use the usual dropdown menu instead. However, for some reason, Gunicorn raises the above error when, as part of get_country_names (), the Country table is called before the news. As soon as I deleted get_country_names () and turned the country field into the Person table into a regular CharField, the problem was solved.

Reading the comments in this old Django bug and this Chase Seibert post helped me a lot in this process.

Despite the fact that ticket number 1796 was corrected more than 6 years ago, it seems that some tiny problems still remain deeply buried there.

Here it is! Thanks to all.

+12
source

Adding to the possible situations in which this occurs. I was looking for a field that cannot be found in any of my models.

Searching by code, I found that I annotated a set of queries with such a field, and then loaded this query as an __in search into another (for other complex queries).

My job was to modify this annotated query to return identifiers and use it. In this particular case, this result was always small, so the list of identifiers was not a problem.

+4
source

I had several ManyToMany relationships that worked one way. I already messed around with my settings and changed the name of the main application several times. Somewhere nearby, I deleted it from the INSTALLED_APPS section! As soon as I added that I was back, then it worked. Definitely PEBKAC, but maybe this will someday help someone. It took me a while to think about this, because the application basically worked.

For example, my application is called deathvalleydogs . I had two models:

 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 Trip that lists Dogs tags that were on the way:

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

then I got the error:

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

Although I still managed to show a template for Dog , which lists the trips in which they were. Note that trips must be a field created by m2m for Dog objects. I did not refer to this field in the template, but in any case, it was included in this field in debug mode.

I want the error to be more obvious, but I'm so happy that I finally found my mistake !!!

+3
source

I used the wrong search in the search field of the admin model, for example:

Does not work:

 class SomeAdmin(admin.ModelAdmin): list_display = ( "id", "thing_id", "count", "stuff_name", "stuff_id" ) readonly_fields = ("count") # These cannot be resolved, because "stuff" doesn't exist on the model search_fields = ("stuff__name", "stuff__id") def stuff_name(self, obj): return obj.thing.stuff.name def stuff_id(self, obj): return obj.thing.stuff.id def get_queryset(self, request): return super().get_queryset(request).select_related("thing") 

Working:

 class SomeAdmin(admin.ModelAdmin): list_display = ( "id", "thing_id", "count", "stuff_name", "stuff_id" ) readonly_fields = ("count") search_fields = ("thing__stuff__name", "thing__stuff__id", "thing__id") def stuff_name(self, obj): return obj.thing.stuff.name def stuff_id(self, obj): return obj.thing.stuff.id def get_queryset(self, request): return super().get_queryset(request).select_related("thing") 
0
source

You can try to reset the migration:

  1. Delete all migration files in your project. Go to each application migration folder of your project ( your_app / migrations / ) and delete everything inside except the init .py file.
  2. Running makemigrations and migrate .
-1
source

Source: https://habr.com/ru/post/955136/


All Articles