[Fixed]-Django query params to array

26๐Ÿ‘

โœ…

I donโ€™t believe this way will work. When you pass the value in querystring, I guess Django will recieve as a string.

โ€“ EDIT โ€“

The above answer wont work is need to add more years, I got the solution 127.0.0.1:8000/blogs?years=2018&years=2019 and

years = self.request.query_params.getlist('years', '')

converts this to list.

โ€“ @indavinci

6๐Ÿ‘

I think a better a better and cleaner way to solve this problem is to just pass the string as comma-separated values in query params and split them up in your Django View.

URL:

http://127.0.0.1:8000/blogs?years=2018,2019

Your View:

def get_queryset(self):
    years = self.request.query_params.get('years').split(',')
    return self.queryset.filter(year__in=years)
๐Ÿ‘คIshant Dahiya

5๐Ÿ‘

If you have function based view this should work:

request.GET.getlist('years', '')
๐Ÿ‘คZhigal

Leave a comment