Bishwas-py/djapy

In path params hamper the object level, and converts the objects into dict, rather than objects.

Closed this issue · 1 comments

path('<int:todo_id>/update', views.update_todo_item, name='update-todo-item'),

class TodoItemUpdateSchema(Schema):
    title: Optional[str] = None
    description: Optional[str] = None
    will_be_completed_at: Optional[datetime] = None
    completed_at: Optional[datetime] = None

@djapify(allowed_method="PUT")
@djapy_auth(AUTH_MECHANISM, permissions=["todo.change_todoitem"])
def update_todo_item(request, todo_id: int, data: TodoItemUpdateSchema) -> TodoItemSchema:
    todo_item = TodoItem.objects.get(id=todo_id)
    if data.title:
        todo_item.title = data.title
    if data.description:
        todo_item.description = data.description
    if data.completed_at:
        todo_item.completed_at = data.completed_at
    if data.will_be_completed_at:
        todo_item.will_be_completed_at = data.will_be_completed_at
    todo_item.save()
    return todo_item

In this situation, todo_id works well, but data gets accepted only with a second level child:

"data": {
   "title": "XYZ"
    ...
}

Also, data acts as dict rather than object after being parsed. data.title is not possible, but data['title'] is.

# this is the input parsed:
{'title': 'Django testing #2', 'description': None, 'will_be_completed_at': datetime.datetime(2024, 3, 11, 23, 17, tzinfo=TzInfo(UTC)), 'completed_at': datetime.datetime(2024, 3, 2, 5, 35, tzinfo=TzInfo(UTC))}

ee8d8fd has fixed the issue.