-
I checked nested-objects, Then I used models.Manager with natural_key, It seems that the serialization succeeded, but it's not what I want. He looks like this: {
"pk": 1,
"model": "store.book",
"fields": {
"name": "Mostly Harmless",
"author": [
"jiang",
"liu"
],
}
} What can I do to get such data {
"pk": 1,
"model": "store.book",
"fields": {
"first_name": "jiang",
"last_name": "liu",
"birth_date": "1952-03-11",
}
} In fastapi, if I use a join query, it will be automatically serialized to the situation I want, |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Looks like you just need to read about nested objects in your response schema. |
Beta Was this translation helpful? Give feedback.
-
As I described, it forced the use of If I want to put the data in schema model, like: class Response(Schema)
code: 200
msg: str = 'Success'
data: Any = None like demo, i need to do this @api.get("/tasks")
def tasks(request):
queryset = Task.objects.select_related("owner")
return Response(data=serialize('json', queryset)) return will be like: {
"code": 200,
"msg": "Success"
"data": [
{
"id": 1,
"title": "Task 1",
"is_completed": false,
"owner": 1 # < --- !!! not resolve
},
{
"id": 2,
"title": "Task 2",
"is_completed": false,
"owner": null
},
]
} |
Beta Was this translation helpful? Give feedback.
As I described, it forced the use of
response=List[Schema]
, but he has only bare dataIf I want to put the data in schema model, like:
like demo, i need to do this
return will be like: