filter a queryset by containing a string

You can use .annotate(…) [Django-doc] to inject the text of the article, and then filter the objects:

from django.db.models import F, Q, TextField, Value

Alert.objects.annotate(
    article_text=Value(article.text, output_field=TextField())
).filter(
    Q(substring=None) |
    Q(substring='') |
    Q(article_text__contains=F('substring'))
)

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top