django-ninja
django-ninja copied to clipboard
[BUG] Field dotted alias is not computed
Describe the bug
Aliases are not returned when it comes to dotted expressions.
I tried to debug by applying the schema directly to my data and it turns out that it's not calculated here either.
I use this example from the documentation to illustrate.
The Schema
from ninja import Field, Schema
class TaskSchema(Schema):
id: int
title: str
completed: bool = Field(..., alias="is_completed")
owner_first_name: str = Field(None, alias="owner.first_name")
The data
tasks = [
{
"id": 1,
"title": "Task 1",
"is_completed": False,
"owner": {
"id": 1,
"first_name": "John",
"last_name": "Doe",
}
},
{
"id": 2,
"title": "Task 2",
"is_completed": False,
"owner": None
},
]
Application
TaskSchema(**tasks[0])
>>> TaskSchema(id=1, title='Task 1', completed=False, owner_first_name=None)
TaskSchema(**tasks[0]).dict()
>>> {'id': 1, 'title': 'Task 1', 'completed': False, 'owner_first_name': None}
According to the documentation, the field should be filled in like this:
>>> {'id': 1, 'title': 'Task 1', 'completed': False, 'owner_first_name': "John"}
Versions (please complete the following information):
- Python version: 3.8.13
- Django version: 4.0.2
- Django-Ninja version: 0.19.0
Hi @Lrakotoson
well it works only when from_orm
is involved:
>>> TaskSchema.from_orm(tasks[0])
TaskSchema(id=1, title='Task 1', completed=False, owner_first_name='John')
but I will take a look - maybe there is a way to use init as well...
Indeed it works with the ORM, thank you 👍.
but I will take a look - maybe there is a way to use init as well...
When you start doing more complex manipulations that require conversion to an ordinary python object (list and dict) towards the end, you lose this feature indeed.