DJANGO Queryset filter using array values

DJANGO


How to filter django queryset using values from an array.

Standard functionality is to use __in

 

in

In a given iterable; often a list, tuple, or queryset. It’s not a common use case, but strings (being iterables) are accepted.

Examples:

Entry.objects.filter(id__in=[1, 3, 4])
Entry.objects.filter(headline__in='abc')

inner_qs = Blog.objects.filter(name__contains='Cheddar')
entries = Entry.objects.filter(blog__in=inner_qs)

inner_qs = Blog.objects.filter(name__contains='Ch').values('name')
entries = Entry.objects.filter(blog__name__in=inner_qs)


ztags = str(zcontent.doc_tags).replace(" ",'').split(",")
zdata_related = docu_lib.objects.filter(doc_tags__in = ztags)

 

Sample

sector_indices=request.POST.getlist('sector_indices')
        for sector in sector_indices:
            lv_sector_selected.append(sector)
        zsymbol = Indices_Master.objects.filter(index_name__in = lv_sector_selected).values('symbol')
        zanalysis = Live_Price_Master.objects.filter(symbol__in = zsymbol).order_by('security_master__industry')

Reference

https://docs.djangoproject.com/en/dev/ref/models/querysets/#in

            Related