If my POST data from a form submission = "John Doe", and I'm trying to match it to an object in this model:
class Person(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
How do I do that?
I've tried:
person = Person.objects.filter(last_name__in=post_data,first_name__in=post_data)
It doesn't work because the "__in" field locator is looking for a list.
I've tried:
person = Person.objects.filter(last_name__in=list(post_data),first_name__in=list(post_data))
It doesn't work because I've created a one-item list with the item being "John Doe", which doesn't match either first_name or last_name in a person object.
Please help out a beginner. Thanks!
You may use something like this:
from django.db.models import Q ... for w in post_data.split(): qs = qs.filter(Q(first_name__icontains=w)|Q(last_name__icontains=w))
It splits the input string, and matches every word against first_name or last_name.