#Best way to handle many optional query parameters

4 messages · Page 1 of 1 (latest)

limber valve
#

What's the best way to handle many optional query parameters for a single endpoint? Currently I am just checking if the argument is available and I have a bunch of if else statements but I am trying to see if there is a better way to implement this. Thanks!

EDIT: I am also not sure what's the best way to use filtering for my database queries here.

        parts = []

        # optional query params start here
        category = category = request.query_params.get('category')
        tags = request.query_params.get('tag')
        price = request.query.params.get('price')

        if price is not None:
            ......
        if tags is None or len(tags) == 0:
            parts = Part.objects.filter(category=category)
        else:
            tag_list = tags.split(",")
            parts = Part.objects.filter(category=category,tags__name__in=tag_list).distinct()```
craggy isle
#

Have you looked into django-filter?

limber valve
limber valve
#

I think I got it to work! thanks Tim. Gonna get off now and finish it tomorrow.