You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I feel like the very first example should import Query to support the example directly under it and to create the underlying understanding for that doc page as you read.
from ninja import FilterSchema, Field, Query
from typing import Optional
class BookFilterSchema(FilterSchema):
name: Optional[str] = None
author: Optional[str] = None
created_after: Optional[datetime] = None
I think you may mention serialization or link to a central doc page talking about the need for it. (Other docs page examples show the serialization.) When I followed the examples as is, I got a json serialization error. When I serialized it, the problem was fixed and my responses came back perfectly.
Also, this was sort of intuitive, but I did have to read the example twice to catch it. You may mention the = None is what makes a field required or not. Just a small note or even comment next to one of the lines.
from ninja import NinjaAPI, Query, Schema
from api.models import People
from django.http import JsonResponse
class PeopleFilterSchema(Schema):
id: int | None = None
first_name: str | None = None
last_name: str | None = None
@api.get("/people")
def list_people(request, filters: PeopleFilterSchema = Query(None)):
queryset = People.objects.all()
if filters.id:
queryset = queryset.filter(id=filters.id)
if filters.first_name:
queryset = queryset.filter(first_name=filters.first_name)
if filters.last_name:
queryset = queryset.filter(last_name=filters.last_name)
# Serialize queryset to JSON
serialized_data = list(queryset.values("id", "first_name", "last_name"))
return JsonResponse(serialized_data, safe=False)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
This is in regard to https://django-ninja.dev/guides/input/filtering/.
I feel like the very first example should import
Query
to support the example directly under it and to create the underlying understanding for that doc page as you read.I think you may mention serialization or link to a central doc page talking about the need for it. (Other docs page examples show the serialization.) When I followed the examples as is, I got a json serialization error. When I serialized it, the problem was fixed and my responses came back perfectly.
Also, this was sort of intuitive, but I did have to read the example twice to catch it. You may mention the
= None
is what makes a field required or not. Just a small note or even comment next to one of the lines.Beta Was this translation helpful? Give feedback.
All reactions