Coder Social home page Coder Social logo

pandaaaa906 / drf-typed Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rsinger86/drf-typed

0.0 0.0 0.0 802 KB

Type hints for enhanced API views and serializers.

Home Page: https://rsinger86.github.io/drf-typed/

License: MIT License

Shell 0.03% Python 99.97%

drf-typed's Introduction

Django REST - Typed

Package version Python versions Python versions PyPI - Django Version

This project extends Django REST Framework to allow use of Python's type hints for automatically validating view parameters, as well as supporting typed attributes and annotation-generated fields on serializers.

Deriving automatic behavior from type annotations has become increasingly popular with the FastAPI and Django Ninja frameworks. The goal of this project is to provide these benefits to the DRF ecosystem.

Main benefits:

  • View inputs can be individually declared, not buried inside all-encompassing request objects.
  • Type annotations can replace repetitive view validation/sanitization code.
  • Simple serializers can have their fields auto-generated from annotations
  • Validated serializer data can be accessed from attributes, with their types known to the IDE
  • Pydantic models are compatible types for view parameters. Annotate your POST/PUT functions with them to automatically validate incoming request bodies.

Documentation: https://rsinger86.github.io/drf-typed

Source Code: https://github.com/rsinger86/drf-typed

Views Example

from rest_typed.views import typed_api_view

"""
GET /users/registered/?registered_on=2019-03-03&staff=yes
"""

@typed_api_view(["GET"])
def get_users(registered_on: date = None, staff: bool = None):
    print(registered_on, is_staff)
    # date(2019, 3, 3) True
    data = query_orm(registered_on, is_staff)
    return Response(data)

Serializers Example

from datetime import date
from rest_typed.serializers import TSerializer


class MovieSerializer(TSerializer):
    title: str          # same as: CharField(required=True, allow_null=False)
    release_date: date  # same as: DateField(required=True, allow_null=False)
    description = None  # same as: DateField(default=None)

movie = MovieSerializer(data={
  "title": "The Last Duel",
  "release_date": "2021-10-15",
})

movie.is_valid(raise_exception=True)

print(movie.validated_data)
"""
  {
    "title": "The Last Duel",
    "release_date": date(2021, 10, 15),
    "description": None
  }
"""

# Or access attributes directly:
print(movie.title) # The Last Duel
print(movie.release_date) # date(2021, 10, 15)

The IDE can help you understand types and auto-complete attributes:

Type Annotation

Type Annotation


Install

Install using:

pip install drf-typed

Python 3.8 or higher is required.

Changelog

0.3.0 (March 2023)

  • Adds support for nested serializers from type annotations.

0.2.0 (January 2022)

  • Fixes setup.py for finding packages

0.1.3 (October 2021)

  • Fixes setup.py for finding packages

0.1.1 (October 2021)

  • Docs improvements
  • Updates setup.py to include stubs package

0.1.0 (October 2021)

  • First release

Testing

Tests are found in a simplified Django project in the /tests folder. Install the project requirements and do ./manage.py test to run them.

License

See License.

drf-typed's People

Contributors

rsinger86 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.