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 request_contract

def create_form_for_user(user, data):
    class MyUserForm(forms.Form):
        # ... 
        if user_can_do_thing:
            my_field = ...

    return MyUserForm(data)


@request_contract({'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(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.12.tar.gz (19.7 kB view details)

Uploaded Source

Built Distribution

django_contracts-0.3.12-py2.py3-none-any.whl (31.1 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

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

Hashes for django_contracts-0.3.12.tar.gz
Algorithm Hash digest
SHA256 458b69a48c2dfda230d7c880a59ebe08d5e13960c33c41b33137724d119b4c54
MD5 5eace904dcedd3e52a95cfc07fdbe08d
BLAKE2b-256 f7dd384802eef1a7096cc324d75b268ddf547189383d5d4dba71d22757389c44

See more details on using hashes here.

File details

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

File metadata

  • Download URL: django_contracts-0.3.12-py2.py3-none-any.whl
  • Upload date:
  • Size: 31.1 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.12-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 e38da4222a612ff26303d64db2cee1b65c3ef8a0b647f19e6508835e0c655135
MD5 2001b87b51462e2022dbb94d15a4e0ee
BLAKE2b-256 f574fc11a98709c990fd01d3907d99221bab5b29ca2ae14fcd7eb7ba6eef0a09

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