pypi django-ninja 0.17.0

latest releases: 1.3.0, 1.2.2, 1.2.1...
2 years ago

This release brings few long awaited features:

Smarter schema

Now you can access orm instance attributes inside schema with resolvers:

class TaskSchema(Schema):
    title: str
    is_completed: bool
    owner: Optional[str]
    lower_title: str

    @staticmethod
    def resolve_owner(obj):  # <------- !!!!!!
        if not obj.owner:
            return
        return f"{obj.owner.first_name} {obj.owner.last_name}"

    def resolve_lower_title(self, obj):   # <-------- !!!!!!
        return self.title.lower()

Field aliases now support django template variables dotted syntax:

class TaskSchema(Schema):
    ...
    last_comment: str = Field(..., alias="comment_set.0.text")

Thanks to @SmileyChris

Pagination output

Now default paginated output returns a dict with items and count

You can now override both input and output schemas for custom pagination:

class CustomPagination(PaginationBase):

    class Input(Schema):
        page: int
        
    class Output(Schema):
        items: List[Any]
        total_pages: int
        current_page: int
    
    def paginate_queryset(self, queryset, pagination: Input, **params):
        return {
            'items': ...,
            'total_pages': ...,
            'current_page': ...,
        }

All updates:

New Contributors

Full Changelog: v0.16.2...v0.17.0

Don't miss a new django-ninja release

NewReleases is sending notifications on new releases.