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, 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


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.3.1.tar.gz (19.5 kB view details)

Uploaded Source

Built Distribution

django_contracts-0.3.1-py2.py3-none-any.whl (30.8 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

  • Download URL: django_contracts-0.3.1.tar.gz
  • Upload date:
  • Size: 19.5 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.3.1.tar.gz
Algorithm Hash digest
SHA256 307319ebffac5b638907a6c37d41e8e274bc16f8b5d6d9d6e6fbcf6a6737a377
MD5 4c06d3933747f465c9b8b8348ad7d485
BLAKE2b-256 6b69db0ef28d207b720a470e0d6fc9eeec0324b7cd1dc883d4b8cc2b65d99e92

See more details on using hashes here.

File details

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

File metadata

  • Download URL: django_contracts-0.3.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 30.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.3.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 f164f67c3d8e87d8086dce3f6afe1e435cc4236ce12a147834ac1cc31ea61bba
MD5 350db85b8cd8574dee0fa6928c3c80f9
BLAKE2b-256 03aa8f3d97e321003335f1e8e42015b73e220f0e0813ac641a21342ce5f78c5d

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