django-contracts-0.4.5
توضیحات
A lightweight API validation library built on top of Django forms.
| ویژگی | مقدار |
|---|---|
| سیستم عامل | - |
| نام فایل | django-contracts-0.4.5 |
| نام | django-contracts |
| نسخه کتابخانه | 0.4.5 |
| نگهدارنده | [] |
| ایمیل نگهدارنده | [] |
| نویسنده | Collin Sage |
| ایمیل نویسنده | clln.sage@yahoo.com |
| آدرس صفحه اصلی | https://github.com/MrSage/django_api_contracts |
| آدرس اینترنتی | https://pypi.org/project/django-contracts/ |
| مجوز | MIT |
# 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
```python
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.
```python
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:
```python
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.
نیازمندی
| مقدار | نام |
|---|---|
| - | django |
زبان مورد نیاز
| مقدار | نام |
|---|---|
| >=3.8.0 | Python |
نحوه نصب
نصب پکیج whl django-contracts-0.4.5:
pip install django-contracts-0.4.5.whl
نصب پکیج tar.gz django-contracts-0.4.5:
pip install django-contracts-0.4.5.tar.gz