Reputation: 10454
I have some data in a django 1.8 table: "key" column values = "a.2", "b.4", "c.6" I want to filter it by ["a", "b"] and ignore the parts after the dot.
Blog.objects.filter(key__startswith=['a','b'])
returns nothing
Blog.objects.filter(key__contains=['a','b'])
returns nothing
Blog.objects.filter(key__in=['a%','b%'])
returns nothing
Is there an efficient way to do this as opposed to looping?
Upvotes: 0
Views: 488
Reputation: 1639
You could try it with Q
objects:
from django.db.models import Q
Blog.objects.filter(Q(key__startswith='a') | Q(key__startswith='b'))
https://docs.djangoproject.com/en/1.8/topics/db/queries/#complex-lookups-with-q
Or using regex
or iregex
:
Blog.objects.filter(key__iregex=r'^(a|b)\.')
Upvotes: 3