22👍
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.
7👍
Adding to the possible situations under which this happens. I searched for the field that could not be found in any of my models.
Searching on the code I found that I was annotating a queryset with such field and then feeding that queryset as an __in
search to another (along other complex queries).
My work around was to change that annotated queryset to return IDs and use that. On this particular case that result was always going to be small so the list of IDs was not a problem to pass.
- [Django]-Can I make an admin field not required in Django without creating a form?
- [Django]-Deploying Google Analytics With Django
- [Django]-Include CSS and Javascript in my django template
3👍
I’d had some ManyToMany relationships that were working one-way. I had been messing around with my settings and changing the name of the main application a couple times. Somewhere along the lines, I had removed it from the INSTALLED_APPS
section! Once I added that back in, then it worked. Definitely PEBKAC, but maybe this will help someone some day. It took me a while to think of checking for that, since the app was mostly working.
For example, my app 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 a Trip
listing the Dogs
that were on the trip like this:
{% for dog in trip.dogs.all %}
<li><a href="/dogs/{{ dog.id }}">{{ dog.name }}</a></li>
{% endfor %}
then I got an error of:
Cannot resolve keyword u'trips' into field. Choices are: active, birth_date, ...
Though I was still able to show a template for a Dog
listing the trips they were on. Notice that trips
should have been a field created by the m2m on the Dog
objects. I wasn’t referencing that field in the template, but it barfed on that field anyway in debug mode.
I wish the error had been more explicit, but I’m just so happy I finally found my mistake!!!
- [Django]-How to get Django AutoFields to start at a higher number
- [Django]-Django queries: how to filter objects to exclude id which is in a list?
- [Django]-Amazon Elastic Beanstalk not serving django static files
0👍
You can try to reset migrations:
- Remove the all migrations files within your project.
Go through each of your projects apps migration folder (your_app/migrations/) and remove everything inside, except the init.py file. - Run
makemigrations
andmigrate
.
- [Django]-How to pass common dictionary data to every page in django
- [Django]-How do I use the built in password reset/change views with my own templates
- [Django]-Custom error message in Django admin actions
0👍
I was using the wrong dunder lookup on an admin model search field, so for example:
Not working:
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")
- [Django]-How to update an object from edit form in Django?
- [Django]-Per Field Permission in Django REST Framework
- [Django]-Accessing "Media" files in Django
0👍
I had this error. And if your are using POSTMAN to do an API call to a URL, then you may be encountering this same error.
django.core.exceptions.FieldError: Cannot resolve keyword ‘player’ into field. Choices are …
Either in your model or serializer, you are referring to a particular field e.g. player
in my case as you would when it is referenced as a foreign key.
In my case I had a Player model, and I wanted to update the reference in the save method of a Market model, when a weight is changed in the market, it should reflect immediately on the player.
class Market(models.Model):
player = models.ForeignKey(Player)
weight = models.CharField('Weight')
...
def save(self):
if self.weight:
# wrong
Player.objects.get(player=self.player) # this object reference of itself does not exist, hence the error
# right
Player.objects.get(pk=self.player.pk)
...
super().save()
- [Django]-Django Rest Framework File Upload
- [Django]-Is the Lift framework as "easy" as Ruby on Rails or Django?
- [Django]-Django sort by distance