简体   繁体   中英

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)

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM