Hasib A.
Hasib A.

Reputation: 73

How to pass multiple key-value pairs as query parameter in Python Django?

I am building a key-value store REST API that will store data in a key value pair. Suppose I want to create an endpoint where I can make a GET request to values/ to get one or more values by passing one or more keys just like this:

/values?keys=key1,key2,key3

I want to know what should I do in urls.py if I want to do this variable length query.

This is my model:

class Data(models.Model):
    key = models.CharField(max_length = 100)
    value = models.CharField(max_length = 1000)

    def __str__(self):
        return '{}:{}'.format(self.key, self.value)

Upvotes: 1

Views: 1396

Answers (1)

Saber Solooki
Saber Solooki

Reputation: 1220

You don't need do anything in your url.py. You just need get your query param (in your case it is keys) in your view.py and you can do this like below:

def your_view(request):
    keys = request.query_params.get('keys')

Upvotes: 3

Related Questions