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

Uploaded Source

Built Distribution

django_contracts-0.1.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.1.0.tar.gz.

File metadata

  • Download URL: django_contracts-0.1.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.1.0.tar.gz
Algorithm Hash digest
SHA256 bc7e632db43aa8244660b4421ef81e5b8fe1269d2580d4c3a4d44dd2928b14d9
MD5 3d4dcb5a5397fd0625e685be32bbf51b
BLAKE2b-256 3ab17c93986976e150e94cc89dbfe10bc3984c30ecb56f6280f011d4610f9162

See more details on using hashes here.

File details

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

File metadata

  • Download URL: django_contracts-0.1.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.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 56f3efabeb38d0ed0dad2313cab066d4c385f1c9580fee92916b8596029912c1
MD5 3e6070794407c526227ad4a43728d911
BLAKE2b-256 939dc21b165bff2d32522fa73c27af3aa2a486a2e2428924fef690d12567f51a

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