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
@apply(MyFormClass, for_method='POST')
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 apply
def create_form_for_user(user, data):
class MyUserForm(forms.Form):
# ...
if user_can_do_thing:
my_field = ...
return MyUserForm(data)
@apply(create_form_for_user, for_method='POST', 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(MyUserForm, for_method='POST', 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.3.0.tar.gz
.
File metadata
- Download URL: django_contracts-0.3.0.tar.gz
- Upload date:
- Size: 16.4 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 | 031b7fdf9a71aae0806f7831a9247944fadb81666bc0dc8becc25f19ed3f1b6d |
|
MD5 | 49e1f8fc058d04ac0b0e0775efe06c39 |
|
BLAKE2b-256 | 655347ae87020507dbb432140ce6a3f627c2e20a5b214d40b65de5033fbc830a |
File details
Details for the file django_contracts-0.3.0-py2.py3-none-any.whl
.
File metadata
- Download URL: django_contracts-0.3.0-py2.py3-none-any.whl
- Upload date:
- Size: 1.9 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 | 9386e0bccc6f099457cf9a6aab0369306ace1abdb6c083f88fef1fb8cb7b919a |
|
MD5 | 0bc5448102be52d34454c019bbde689a |
|
BLAKE2b-256 | 79f11b1e5289c12b2719806432513b0d45fcccfdd95f37136362da497e2dcdad |