user3067444
user3067444

Reputation: 29

Django - Cannot resolve keyword

I am trying to filter results in a django view using a function as follows:

views.py

def index(request):
    european_team_list = Team.objects.all().filter(type = 'Europe')
    context = {'european_team_list': european_team_list}
    return render(request, 'myapp/index.html', context)

admin.py

class Team(models.Model):
    continent = models.CharField()
    def _team_type(self):
        if self.country = "Europe":
            return "Europe"
        else:
            return "Not Europe"
    team_type = property(_team_type)
    ...other fields...

However, when I load the page, I get an error "Cannot resolve keyword 'team_type' into field. Choices are:" and then it lists all the fields in the Team class other than team_type. Any guidance would be much appreciated.

Upvotes: 0

Views: 1108

Answers (1)

Kevin Christopher Henry
Kevin Christopher Henry

Reputation: 49032

The simple answer is that you can't do this with the filter() method. filter() is used to construct SQL queries and can only operate on objects at the database level.

So you should figure out how to phrase your query using the database values. It's not clear what your actual code is, but it might look something like:

european_team_list = Team.objects.filter(continent='Europe')

or:

european_team_list = Team.objects.filter(country__in=('France', 'Poland'))

Upvotes: 1

Related Questions