django rest框架-redux表单连接器
drfrf的Python项目详细描述
DRFRF
django rest framework-redux表单连接器
安装
$ pipenv install drfrf
用法
导入并使用ValidateCreateMixin
或ValidateUpdateMixin
之一:
fromdrfrfimportValidateCreateMixin,ValidateUpdateMixinfromrest_frameworkimportviewsetsfromrest_framework.decoratorsimportdetail_route,list_routefromshop.modelsimportCartItemfromshop.serializersimportCartItemSerializerclassCartItemViewSet(ValidateCreateMixin,ValidateUpdateMixin,viewsets.ModelViewSet):queryset=CartItem.objects.all()serializer_class=CartItemSerializer@list_route(methods=["post"],url_path="validate")defvalidate_list(self,request):returnself.validate_create(request)@detail_route(methods=["post"],url_path="validate")defvalidate_detail(self,request,**kwargs):returnself.validate_update(request,**kwargs)
将无效数据发布到/items/validate
将返回错误消息:
HTTP/1.1400Bad RequestContent-Length:99Content-Type:application/json{"amount":["Your current credit is 36€, please top-up your account before adding to your cart."]}
在这里浏览完整的示例:https://github.com/Theodo-UK/drfrf/tree/master/example/backend/shop
API
ValidateCreateMixin
公开一个方法:validate_create
接受Request,根据提供的序列化程序验证输入数据并返回Response
ValidateUpdateMixin
公开一个方法:validate_update
使用kwargs获取Request,根据提供的序列化程序验证输入数据并返回Response
前端集成
如果您在前端使用redux表单,请签出drfrf for the front