模型清理方法和验证错误连接的混合
django-model-cleanup的Python项目详细描述
- 自由软件:麻省理工学院许可证
- 文档:https://django-model-cleanup.readthedocs.io。
- 提供干净的方法实现
- 调用完全清除将导致调用所有清除方法
- 无论验证错误如何,都将调用所有方法-一次获取所有错误
- 如果错误没有错误,则根据干净的方法名自动将错误映射到字段名
快速启动
安装django模型清理程序:
pip install django-model-cleanup
在您的模型中添加MIXIN,并在 不再需要每次错误处理和连接:fromdjango.core.exceptionsimportValidationErrorfromdjango.dbimportmodelsfromdjango.utils.translationimportugettext_lazyas_fromdjango_model_cleanupimportCleanMixin,ExtensibleValidationErrorclassSomeModel(CleanMixin,models.Model):lorem=models.CharField(max_length=10,blank=True)defclean_foo(self):raiseValidationError('Foo is bad')defclean_bar(self):raiseExtensibleValidationError({'bar':_('Bar is wrong cause %s > %s!')},code='bar',params=(2,1))defclean_legacy(self):# We can't init ValidationError as one-liner, cause dict + params are not compatible# We need to wrap a message in ValidationError and put that in dict indicating a fieldmsg=_('Bar legacy error %s > %s!')error=ValidationError(msg,code='bar',params=(7,5))raiseValidationError({'bar':error})
# This is not longer required:defclean(self):errors=[]try:self.clean_foo()exceptValidationErrorasex:errors.append(ex)errors=[]try:self.clean_bar()exceptValidationErrorasex:errors.append(ex)errors=[]try:self.clean_legacy()exceptValidationErrorasex:errors.append(ex)iferrors:raiseValidationError(errors)
运行测试
代码真的有用吗?
- ::
- $pipenv安装–开发 $pipenv外壳 $tox
我们建议使用pipenv,但创建virtualenv和安装需求的传统方法也应该有效。 请安装requirements/development.txt以设置用于测试和开发的虚拟环境。
学分
这个包是用Cookiecutter和wooyek/cookiecutter-django-app项目模板创建的。
历史记录
0.1.0(2017-12-05)
- pypi上的第一个版本。