# Django REST - Typed
[![Package version](https://badge.fury.io/py/drf-typed.svg)](https://pypi.python.org/pypi/drf-typed)
[![Python versions](https://img.shields.io/pypi/status/drf-typed.svg)](https://img.shields.io/pypi/status/drf-typed.svg/)
[![Python versions](https://img.shields.io/pypi/pyversions/drf-typed.svg)](https://pypi.org/project/drf-typed/)
![PyPI - Django Version](https://img.shields.io/pypi/djversions/drf-typed)
This project extends [Django REST Framework](https://www.django-rest-framework.org/) 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](https://fastapi.tiangolo.com/) and [Django Ninja](https://django-ninja.rest-framework.com/) 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](https://pydantic-docs.helpmanual.io/) models are compatible types for view parameters. Annotate your POST/PUT functions with them to automatically validate incoming request bodies.
**Documentation**: <a href="https://rsinger86.github.io/drf-typed/" target="_blank">https://rsinger86.github.io/drf-typed</a>
**Source Code**: <a href="https://github.com/rsinger86/drf-typed/" target="_blank">https://github.com/rsinger86/drf-typed</a>
## Views Example
```python
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
```python
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](docs/images/attribute-str-type-hint.jpg)
![Type Annotation](docs/images/attribute-date-auto-complete.jpg)
---
# 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](LICENSE.md).
Raw data
{
"_id": null,
"home_page": "https://github.com/rsinger86/drf-typed",
"name": "drf-typed",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "django rest type annotations automatic validation validate",
"author": "Robert Singer",
"author_email": "robertgsinger@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/05/78/44d6ce3787d15fd8eb907a3c69f18dc6b299b0a1dcb2716d3103a05b65fc/drf-typed-0.3.0.tar.gz",
"platform": null,
"description": "# Django REST - Typed\n\n[![Package version](https://badge.fury.io/py/drf-typed.svg)](https://pypi.python.org/pypi/drf-typed)\n[![Python versions](https://img.shields.io/pypi/status/drf-typed.svg)](https://img.shields.io/pypi/status/drf-typed.svg/)\n[![Python versions](https://img.shields.io/pypi/pyversions/drf-typed.svg)](https://pypi.org/project/drf-typed/)\n![PyPI - Django Version](https://img.shields.io/pypi/djversions/drf-typed)\n\nThis project extends [Django REST Framework](https://www.django-rest-framework.org/) 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.\n\nDeriving automatic behavior from type annotations has become increasingly popular with the [FastAPI](https://fastapi.tiangolo.com/) and [Django Ninja](https://django-ninja.rest-framework.com/) frameworks. The goal of this project is to provide these benefits to the DRF ecosystem.\n\nMain benefits:\n\n- View inputs can be individually declared, not buried inside all-encompassing `request` objects.\n- Type annotations can replace repetitive view validation/sanitization code.\n- Simple serializers can have their fields auto-generated from annotations\n- Validated serializer data can be accessed from attributes, with their types known to the IDE\n- [Pydantic](https://pydantic-docs.helpmanual.io/) models are compatible types for view parameters. Annotate your POST/PUT functions with them to automatically validate incoming request bodies.\n\n**Documentation**: <a href=\"https://rsinger86.github.io/drf-typed/\" target=\"_blank\">https://rsinger86.github.io/drf-typed</a>\n\n**Source Code**: <a href=\"https://github.com/rsinger86/drf-typed/\" target=\"_blank\">https://github.com/rsinger86/drf-typed</a>\n\n## Views Example\n\n```python\nfrom rest_typed.views import typed_api_view\n\n\"\"\"\nGET /users/registered/?registered_on=2019-03-03&staff=yes\n\"\"\"\n\n@typed_api_view([\"GET\"])\ndef get_users(registered_on: date = None, staff: bool = None):\n print(registered_on, is_staff)\n # date(2019, 3, 3) True\n data = query_orm(registered_on, is_staff)\n return Response(data)\n```\n\n## Serializers Example\n\n```python\nfrom datetime import date\nfrom rest_typed.serializers import TSerializer\n\n\nclass MovieSerializer(TSerializer):\n title: str # same as: CharField(required=True, allow_null=False)\n release_date: date # same as: DateField(required=True, allow_null=False)\n description = None # same as: DateField(default=None)\n\nmovie = MovieSerializer(data={\n \"title\": \"The Last Duel\",\n \"release_date\": \"2021-10-15\",\n})\n\nmovie.is_valid(raise_exception=True)\n\nprint(movie.validated_data)\n\"\"\"\n {\n \"title\": \"The Last Duel\",\n \"release_date\": date(2021, 10, 15),\n \"description\": None\n }\n\"\"\"\n\n# Or access attributes directly:\nprint(movie.title) # The Last Duel\nprint(movie.release_date) # date(2021, 10, 15)\n```\n\nThe IDE can help you understand types and auto-complete attributes:\n\n![Type Annotation](docs/images/attribute-str-type-hint.jpg)\n\n![Type Annotation](docs/images/attribute-date-auto-complete.jpg)\n\n---\n\n# Install\n\nInstall using:\n\n```\npip install drf-typed\n\n```\n\nPython 3.8 or higher is required.\n\n# Changelog\n\n## 0.3.0 (March 2023)\n\n- Adds support for nested serializers from type annotations.\n\n## 0.2.0 (January 2022)\n\n- Fixes setup.py for finding packages\n\n## 0.1.3 (October 2021)\n\n- Fixes setup.py for finding packages\n\n## 0.1.1 (October 2021)\n\n- Docs improvements\n- Updates setup.py to include stubs package\n\n## 0.1.0 (October 2021)\n\n- First release\n\n# Testing\n\nTests are found in a simplified Django project in the `/tests` folder. Install the project requirements and do `./manage.py test` to run them.\n\n# License\n\nSee [License](LICENSE.md).\n\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Use type annotations for request validation and serializer fields in Django REST Framework",
"version": "0.3.0",
"split_keywords": [
"django",
"rest",
"type",
"annotations",
"automatic",
"validation",
"validate"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "057844d6ce3787d15fd8eb907a3c69f18dc6b299b0a1dcb2716d3103a05b65fc",
"md5": "a9b76aa29491846a1d1ca936ba69030f",
"sha256": "a128e64dc66d4cc865324fd9bfeb88754ef8794d68bc43d6a2f123a4b1e0f97f"
},
"downloads": -1,
"filename": "drf-typed-0.3.0.tar.gz",
"has_sig": false,
"md5_digest": "a9b76aa29491846a1d1ca936ba69030f",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 38723,
"upload_time": "2023-03-25T20:32:15",
"upload_time_iso_8601": "2023-03-25T20:32:15.276986Z",
"url": "https://files.pythonhosted.org/packages/05/78/44d6ce3787d15fd8eb907a3c69f18dc6b299b0a1dcb2716d3103a05b65fc/drf-typed-0.3.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-03-25 20:32:15",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "rsinger86",
"github_project": "drf-typed",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [],
"lcname": "drf-typed"
}