Replies: 4 comments 4 replies
-
Hi @EMUNES Try the folloing: from typing import List
from ninja import Schema, Field
class CommentSchema(Schema):
# define your comment attrs here:
id: int
message: str
class PostSchema(Schema):
id: str
title: str
comments: List[CommentSchema] = Field(..., alias='postcomment_set') # postcomment_set is based on your model or related attr
@api.get('/posts', response=List[PostSchema])
def list_posts(reqiest):
return Post.objects.all() |
Beta Was this translation helpful? Give feedback.
-
Please add it to document |
Beta Was this translation helpful? Give feedback.
-
Does nested objects support support async? |
Beta Was this translation helpful? Give feedback.
-
I tried
|
Beta Was this translation helpful? Give feedback.
-
It's pretty convenient to return objects with nested foreign-key objects using ninja. However, I find no tutorial to return objects including reverse related objects using ninja.
Say I have a Django PostComment model whose foreign-key field points to Post object. And for API I want to return a list of Post objects each includes all comments that points to it. Is it possible to do so using Schemas and if so how should I customize the api code?
I'm new to ninja, Thanks for any hint :)
Beta Was this translation helpful? Give feedback.
All reactions