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

Time:11-04

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

CodePudding user response:

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
  • Related