[Django]-Django, filter records from model method?

2👍

Since you need to filter the queryset object, you can do something like this in your view:

from django.db.models import Q
matches = ['cat', 'kitty', 'meow']
messages = Message.objects.filter(reduce(operator.or_, (Q(message__contains=match) for match in matches)))  #Or use icontains if you want a case insensitive match. 

1👍

The filter should be a method of a MessageManager. See here: https://docs.djangoproject.com/en/dev/topics/db/managers/

Leave a comment