额外的django字段验证。
项目描述
django-extra-field-validation
目录
背景
本软件包旨在提供定义自定义字段验证逻辑所需的工具,这些工具可以独立使用,也可以与 django forms、测试用例、API 实现或任何需要将数据保存到数据库的模型操作一起使用。
如有需要,还可以通过定义检查约束进行扩展,但目前验证仅在模型级别进行处理。
安装
pip install django-extra-field-validation
用法
要求所有字段
from django.db import models
from extra_validator import FieldValidationMixin
class TestModel(FieldValidationMixin, models.Model):
amount = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True)
fixed_price = models.DecimalField(max_digits=7, decimal_places=2, null=True, blank=True)
percentage = models.DecimalField(max_digits=3, decimal_places=0, null=True, blank=True)
REQUIRED_FIELDS = ['amount'] # Always requires an amount to create the instance.
示例
In [1]: from decimal import Decimal
In [2]: from demo.models import TestModel
In [3]: TestModel.objects.create(fixed_price=Decimal('3.00'))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
...
ValueError: {'amount': ValidationError([u'Please provide a value for: "amount".'])}
要求集合中至少有一个字段
from django.db import models
from extra_validator import FieldValidationMixin
class TestModel(FieldValidationMixin, models.Model):
amount = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True)
fixed_price = models.DecimalField(max_digits=7, decimal_places=2, null=True, blank=True)
percentage = models.DecimalField(max_digits=3, decimal_places=0, null=True, blank=True)
REQUIRED_TOGGLE_FIELDS = [
['amount', 'fixed_price', 'percentage'], # Require only one of the following fields.
]
示例
In [1]: from decimal import Decimal
In [2]: from demo.models import TestModel
In [3]: TestModel.objects.create(amount=Decimal('2.50'), fixed_price=Decimal('3.00'))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
...
ValueError: {'fixed_price': ValidationError([u'Please provide only one of: Amount, Fixed price, Percentage'])}
可选地要求集合中至少有一个字段
from django.db import models
from extra_validator import FieldValidationMixin
class TestModel(FieldValidationMixin, models.Model):
amount = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True)
fixed_price = models.DecimalField(max_digits=7, decimal_places=2, null=True, blank=True)
percentage = models.DecimalField(max_digits=3, decimal_places=0, null=True, blank=True)
OPTIONAL_TOGGLE_FIELDS = [
['fixed_price', 'percentage'] # Optionally validates that only fixed price/percentage are provided when present.
]
示例
In [1]: from decimal import Decimal
In [2]: from demo.models import TestModel
In [3]: first_obj = TestModel.objects.create(amount=Decimal('2.0'))
In [4]: second_obj = TestModel.objects.create(amount=Decimal('2.0'), fixed_price=Decimal('3.00'))
In [5]: third_obj = TestModel.objects.create(amount=Decimal('2.0'), fixed_price=Decimal('3.00'), percentage=Decimal('10.0'))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
...
ValueError: {'percentage': ValidationError([u'Please provide only one of: Fixed price, Percentage'])}
条件性要求所有字段
from django.db import models
from django.conf import settings
from extra_validator import FieldValidationMixin
class TestModel(FieldValidationMixin, models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL)
amount = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True)
fixed_price = models.DecimalField(max_digits=7, decimal_places=2, null=True, blank=True)
percentage = models.DecimalField(max_digits=3, decimal_places=0, null=True, blank=True)
CONDITIONAL_REQUIRED_FIELDS = [
(
lambda instance: instance.user.is_active, ['amount', 'percentage'],
),
]
示例
In [1]: from decimal import Decimal
in [2]: from django.contrib.auth import get_user_model
In [3]: from demo.models import TestModel
In [4]: user = get_user_model().objects.create(username='test', is_active=True)
In [5]: first_obj = TestModel.objects.create(user=user, amount=Decimal('2.0'))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
...
ValueError: {u'percentage': ValidationError([u'Please provide a value for: "percentage"'])}
条件性要求集合中至少有一个字段
from django.db import models
from django.conf import settings
from extra_validator import FieldValidationMixin
class TestModel(FieldValidationMixin, models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL)
amount = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True)
fixed_price = models.DecimalField(max_digits=7, decimal_places=2, null=True, blank=True)
percentage = models.DecimalField(max_digits=3, decimal_places=0, null=True, blank=True)
CONDITIONAL_REQUIRED_TOGGLE_FIELDS = [
(
lambda instance: instance.user.is_active, ['fixed_price', 'percentage', 'amount'],
),
]
示例
In [1]: from decimal import Decimal
in [2]: from django.contrib.auth import get_user_model
In [3]: from demo.models import TestModel
In [4]: user = get_user_model().objects.create(username='test', is_active=True)
In [5]: first_obj = TestModel.objects.create(user=user)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
...
ValueError: {'__all__': ValidationError([u'Please provide a valid value for any of the following fields: Fixed price, Percentage, Amount'])}
In [6]: second_obj = TestModel.objects.create(user=user, amount=Decimal('2'), fixed_price=Decimal('2'))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
...
ValueError: {'__all__': ValidationError([u'Please provide only one of the following fields: Fixed price, Percentage, Amount'])}
模型属性
这是通过以下模型属性完成的。
# A list of required fields
REQUIRED_FIELDS = []
# A list of fields with at most one required.
REQUIRED_TOGGLE_FIELDS = []
# A list of field with at least one required.
REQUIRED_MIN_FIELDS = []
# Optional list of fields with at most one required.
OPTIONAL_TOGGLE_FIELDS = []
# Conditional field required list of tuples the condition a boolean or a callable.
# [(lambda user: user.is_admin, ['first_name', 'last_name'])] : Both 'first_name' or 'last_name'
# If condition is True ensure that all fields are set
CONDITIONAL_REQUIRED_FIELDS = []
# [(lambda user: user.is_admin, ['first_name', 'last_name'])] : Either 'first_name' or 'last_name'
# If condition is True ensure that at most one field is set
CONDITIONAL_REQUIRED_TOGGLE_FIELDS = []
# [(lambda user: user.is_admin, ['first_name', 'last_name'])] : At least 'first_name' or 'last_name' provided or both
# If condition is True ensure that at least one field is set
CONDITIONAL_REQUIRED_MIN_FIELDS = []
# [(lambda user: user.is_admin, ['first_name', 'last_name'])] : Both 'first_name' and 'last_name' isn't provided
# If condition is True ensure none of the fields are provided
CONDITIONAL_REQUIRED_EMPTY_FIELDS = []
许可协议
django-extra-field-validation 在 MIT 许可证和 Apache 许可证 Version 2.0 的条款下发布
任选其一。
待办事项
- [ ] 支持
CONDITIONAL_NON_REQUIRED_TOGGLE_FIELDS
- [ ] 支持
CONDITIONAL_NON_REQUIRED_FIELDS
- [ ] 移至支持基于实例对象的类和函数式验证器,这将启用跨字段模型验证。
项目详情
关闭
django-extra-field-validation-1.2.3.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 7e682290b33082be665b08b427e2ab0c6b729569f480a52e785e473e716c8458 |
|
MD5 | cb77587cbdd4381308c35311b4298b28 |
|
BLAKE2b-256 | 0ed397b16a728579da3de1e047be9aed2f294a66b47974c1645618ecb9e07492 |
关闭
django_extra_field_validation-1.2.3-py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | e6bc1838b51456f2e4120e550357e3732812fce833d8b5329a953833ca1406fe |
|
MD5 | 57e910ae61f406dc54811c0ad7098b25 |
|
BLAKE2b-256 | 4f0432af1303b196027c9071fc7817f4a2f2b3c460465be6b2c8e06cc8107953 |