Is there a way to pass a string which contain muliple delimiter in Django?

Question:

Info: I want to make a queryset where i have pass a string. A string contain multiple delimiter , like a separator. I just want to know it is possible to pass a string with muliple delimiter and filter out the video which are match with the strings?

def videosfilter(request):
    ''' videos filter '''

    videos = Video.objects.filter(title__icontains='python programming,django framework')

    return videos
Asked By: afi

||

Answers:

You can use reduce an operator:

from functools import reduce
import operator
from django.db.models import Q
....

def videosfilter(request):    
    my_string = 'python programming,django framework'
    custom_filter = reduce(operator.or_, (Q(title__icontains=x) for x in my_string.split(,)))
    videos = Video.objects.filter(custom_filter)
    return videos
Answered By: LaCharcaSoftware
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.