Query Parameters
जब आप ऐसे function parameters घोषित करते हैं जो path parameters का हिस्सा नहीं हैं, तो उन्हें स्वचालित रूप से "query" parameters के रूप में व्याख्यायित किया जाता है।
weapons = ["Ninjato", "Shuriken", "Katana", "Kama", "Kunai", "Naginata", "Yari"]
@api.get("/weapons")
def list_weapons(request, limit: int = 10, offset: int = 0):
return weapons[offset: offset + limit]
इस operation को query करने के लिए, आप इस तरह का एक URL उपयोग कर सकते हैं:
http://localhost:8000/api/weapons?offset=0&limit=10
वही लाभ जो path parameters पर लागू होते हैं, वे query parameters पर भी लागू होते हैं:
- Editor समर्थन (जाहिर है)
- Data "parsing"
- Data validation
- Automatic दस्तावेज़ीकरण
Note
यदि आप अपने parameters को annotate नहीं करते, तो उन्हें str type माना जाएगा
@api.get("/weapons")
def list_weapons(request, limit, offset):
# type(limit) == str
# type(offset) == str
डिफ़ॉल्ट मान
चूँकि query parameters path का एक निश्चित हिस्सा नहीं होते, वे optional होते हैं और उनके डिफ़ॉल्ट मान हो सकते हैं:
@api.get("/weapons")
def list_weapons(request, limit: int = 10, offset: int = 0):
return weapons[offset : offset + limit]
ऊपर दिए गए उदाहरण में, हमने offset=0 और limit=10 के डिफ़ॉल्ट मान सेट किए हैं।
तो, इस URL पर जाना:
http://localhost:8000/api/weapons
http://localhost:8000/api/weapons?offset=0&limit=10
http://localhost:8000/api/weapons?offset=20
तो आपके function में parameter मान होंगे:
offset=20(क्योंकि आपने इसे URL में सेट किया है)limit=10(क्योंकि वह डिफ़ॉल्ट मान है)
Required और Optional Parameters
आप required या optional GET parameters को उसी तरह घोषित कर सकते हैं जिस तरह आप Python function parameters घोषित करते हैं:
weapons = ["Ninjato", "Shuriken", "Katana", "Kama", "Kunai", "Naginata", "Yari"]
@api.get("/weapons/search")
def search_weapons(request, q: str, offset: int = 0):
results = [w for w in weapons if q in w.lower()]
return results[offset : offset + 10]
इस स्थिति में, Django Ninja हमेशा GET request में पास किए गए q parameter को validate करेगा, जबकि offset parameter एक optional पूर्णांक है।
GET Parameter Type Conversion
आइए कई typed parameters घोषित करें:
from datetime import date
@api.get("/example")
def example(request, s: str = None, b: bool = None, d: date = None, i: int = None):
return [s, b, d, i]
str type as-is पास किया जाता है।
bool type के लिए, निम्नलिखित सभी:
http://localhost:8000/api/example?b=1
http://localhost:8000/api/example?b=True
http://localhost:8000/api/example?b=true
http://localhost:8000/api/example?b=on
http://localhost:8000/api/example?b=yes
b का bool मान True है, अन्यथा False।
Dates date strings और पूर्णांक (Unix timestamps) हो सकते हैं:
http://localhost:8000/api/example?d=1577836800 # equivalent to 2020-01-01 http://localhost:8000/api/example?d=2020-01-01
Schema का उपयोग करना
GET parameters को समाहित करने के लिए आप एक schema भी उपयोग कर सकते हैं:
import datetime
from typing import List
from pydantic import Field
from ninja import Query, Schema
class Filters(Schema):
limit: int = 100
offset: int = None
query: str = None
category__in: List[str] = Field(None, alias="categories")
@api.get("/filter")
def events(request, filters: Query[Filters]):
return {"filters": filters.dict()}
अधिक जटिल filtering परिदृश्यों के लिए, Filtering देखें।
Done
अगले अध्याय पर जाएँ! Request Body
टिप्पणियाँ
पेज व्यूज़:
