[Django]-Django: filtering queryset by "parameter has part of field's value"?

3👍

I think I’ve found a horrible way to do it in the ORM without needing raw SQL. Hopefully someone else will be able to find something better.

from django.db.models import ExpressionWrapper, CharField, Value, F

param = 'Starbucks Pike Place'
myparam = ExpressionWrapper(Value(param), output_field=CharField())
Place.objects.annotate(param=myparam).filter(param__contains=F('username'))
👤wim

0👍

Place.objects.get(name__contains=PARAM) should do it. Note that you can also use icontains instead of contains if you’d like the query to be case-insensitive.

Leave a comment