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


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

Uploaded Source

Built Distribution

django_contracts-0.4.2-py2.py3-none-any.whl (32.5 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

  • Download URL: django_contracts-0.4.2.tar.gz
  • Upload date:
  • Size: 18.8 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.4.2.tar.gz
Algorithm Hash digest
SHA256 db6160bb40a2e21378d9a14d65bae019f735d6e012c50f63683c79c2073eeb70
MD5 6244ed77b377f09f9643e0dd012eea8c
BLAKE2b-256 2b6a2f029c6f850638ae597f708c23c7699065c82ed3b2e5079161c30df217b3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: django_contracts-0.4.2-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

Hashes for django_contracts-0.4.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 0cc0aa2de6a78a6e28960c08943cf03e0d91b72ec8e4016161d203bc3f624c26
MD5 6a0b2c0a57d1d93fe689ae152fde4df4
BLAKE2b-256 ff0c94d57956ec52d877752793b174296e7e1ee36df89e8ac7a09708b2001f38

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