A lightweight API validation library built on top of Django forms.
Project description
Django Contracts
This is a lightweight (and hopefully minimally opinionated) library that exists to make it easier to apply Django Forms to your views. If you need more complex capabilities than what Django Forms offers then you might want to consider something like Django REST Framework.
Basic Usage
from django_contracts.contracts import apply_contract
@apply_contract(request_contracts={'POST': MyRequestForm}, response_contracts={'POST': MyResponseForm})
def my_view(request):
# If you get here then the request matched your contract
data = request.validated_data
# ... do something with the data
You can also supply a function which returns a Django Form class to use as a contract. This is helpful if you need a form to be specific to a user.
from django import forms
from django_contracts.contracts import request_contract
def create_form_for_user(user, data):
class MyUserForm(forms.Form):
# ...
if user_can_do_thing:
my_field = ...
return MyUserForm(data)
@apply_contract(request_contracts={'POST': create_form_for_user}, pass_in_user=True)
def my_view(request):
# ...
Or if you prefer to override init on the form:
from django import forms
from django_contracts.contracts import apply
class MyUserForm(forms.Form):
def __init__(self, user, *args, **kwargs):
super(*args, **kwargs)
self.user = user
@apply_contract(request_contracts={'POST': create_form_for_user}, pass_in_user=True)
def my_view(request)
# ...
Using forms that take a User
can be useful for filtering choice options.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django_contracts-0.4.1.tar.gz
.
File metadata
- Download URL: django_contracts-0.4.1.tar.gz
- Upload date:
- Size: 18.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d7ff101718925282fe1b65f9058013a256927fd8027edc8c5fd2969fc6b9aebb |
|
MD5 | ee9ada40e473ed5e40454f1c5c7ad685 |
|
BLAKE2b-256 | 254f2de74da632cfabb50cd1d1d01753db458a9a15660030c1f867d920ad8bad |
File details
Details for the file django_contracts-0.4.1-py2.py3-none-any.whl
.
File metadata
- Download URL: django_contracts-0.4.1-py2.py3-none-any.whl
- Upload date:
- Size: 32.5 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | aba665c6541fade30ac7e7b6bad9428018cf5729c15c47d24f5e67f226291d2a |
|
MD5 | 7e80c34554156b7e95323d29c882a040 |
|
BLAKE2b-256 | 480564677f60fd7ef915d4c91f7dcc861c501624dd04246c08d2dd98002e1f1d |