user3792705
user3792705

Reputation: 617

How to search with partial exact match in Django

Table definition:

class Hello(models.Model):
  name models.CharField(max_length=64, blank=True)

2 records are in db:

#1 aaaa-bbbb (cd)
#2  aaa-bbbb (cef)

The search key word: 'aaa-bbbb', or 'aaa-bbbb (c',

ret = models.Hello.objects.filter(Q(name__icontains='aaa-bbbb'))

Expected result is #2, but 2 records are found.

How to find #2?

Upvotes: 0

Views: 1437

Answers (1)

dipak
dipak

Reputation: 107

Instead of icontains use iexact like this:

ret = models.Hello.objects.filter(name__iexact='aaa-bbbb')

You don't need to use Q if you are filtering through only one field

Upvotes: 1

Related Questions