Reputation: 45
For a search function in my Django application, I'm using the following statement to query the database for fields containing a search term:
search_results = Post.objects.filter(name_icontains=search_term)
However, I get this error:
Cannot resolve keyword 'name_icontains' into field. Choices are: content, date, id, name, url, views
These are the contents of the models.py file for reference:
import datetime
from django.db import models
class Post(models.Model):
name = models.CharField(max_length=200)
content = models.TextField()
date = models.DateField(default=None, blank=True, null=True)
url = models.CharField(max_length=5, default=None, blank=True, null=True)
views = models.IntegerField(default=0)
def __str__(self):
return self.name
I'm following the method for searching exactly as it's described in the documentation. What am I doing wrong?
Upvotes: 1
Views: 2337
Reputation: 5968
Try doing it with double underscore:
search_results = Post.objects.filter(name__icontains=search_term)
___________________________________________^
See the docs for field lookups in Django for more detailed information about icontains
and other filters.
Upvotes: 4