Skip to main content

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):
    class MyUserForm(forms.Form):
        # ... 

    return MyUserForm


@apply(create_form_for_user, for_method='POST')
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(lambda user: lamba data: MyUserForm(user, data))
def my_view(request)
    # ...

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_contracts-0.2.0.tar.gz (14.3 kB view details)

Uploaded Source

Built Distribution

django_contracts-0.2.0-py2.py3-none-any.whl (1.8 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django_contracts-0.2.0.tar.gz.

File metadata

  • Download URL: django_contracts-0.2.0.tar.gz
  • Upload date:
  • Size: 14.3 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

Hashes for django_contracts-0.2.0.tar.gz
Algorithm Hash digest
SHA256 0359fef84e355cb564e2b4307dd5adad35b6ce63fee8cba2175a471e44415775
MD5 7eda023eccdc12b4e01ea5486b97b03f
BLAKE2b-256 2fcd045e130c5a85559baf70a20b95610ca968ebc8a9de27159e493bd7fb0ca1

See more details on using hashes here.

File details

Details for the file django_contracts-0.2.0-py2.py3-none-any.whl.

File metadata

  • Download URL: django_contracts-0.2.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 1.8 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

Hashes for django_contracts-0.2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 1a658c317336fa04c3c4fe99d8053e968033e3b6b557e48b2c5d7ddacde00a03
MD5 ff9f9eb0202317ce4a8f7d3005aa7884
BLAKE2b-256 a78d8de86ed7c27cea8943446bbe6556926f388ea1448b2b3edc8c61e9230c81

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page