I am trying to filter through a TextField where I have stripped it of its HTML tags. However, it gives me this error: “Cannot resolve keyword ‘search’ into field.” Here is my code:
models.py
JavaScript
x
8
1
class Entry(models.Model):
2
body = models.TextField()
3
4
def search_body(self):
5
tags = re.compile('<.*?>')
6
cleantext = re.sub(tags, '', self.body)
7
return cleantext
8
views.py
JavaScript
1
9
1
from django.db.models import Q
2
from .models import Entry
3
4
5
def search_list(request, query=None):
6
search = "search"
7
entrys = Entry.objects.filter(status="publish").filter(Q(search_body__icontains=search)).distinct()
8
9
Is there a way to do this?
Advertisement
Answer
I just found a way to get what I’m aiming at. All I have to do is to override the “save” function, as so:
JavaScript
1
4
1
def save(self, *args, **kwargs):
2
self.search_body = clean_text(self)
3
super().save(*args, **kwargs)
4
Thanks for your help.