Bishwas-py / djapy

No bullshit, Django Rest API Framework
https://djapy.io
59 stars 3 forks source link

Returning empty query set results `Input should be a valid dictionary or object to extract fields from` #20

Closed Bishwas-py closed 4 months ago

Bishwas-py commented 4 months ago

Let's assume below view_func returns empty data:

@djapify
@paginate(OffsetLimitPagination)
def get_post_comments(request, source_id: int, **kwargs) -> list[CommentSchema]:
    post = Post.objects.get(id=source_id)
    comments_and_replies = PolymorphicComments.objects.filter(
        parent_type=ContentType.objects.get_for_model(post),
        parent_id=post.id
    )
    return comments_and_replies.alive()
{"error": [{"type": "model_attributes_type", "loc": ["response"], "msg": "Input should be a valid dictionary or object to extract fields from", "input": [], "url": "https://errors.pydantic.dev/2.5/v/model_attributes_type"}], "error_count": 1, "title": "output"}

Can be fixed by:

In pagination/offset_pagination.py, line 46, replace return [] with:

return {
                    "items": [],
                    "offset": offset,
                    "limit": limit,
                    "has_next": False,
                    "has_previous": False,
                    "total_pages": 0,
                }