【问题标题】:Tastypie FormValidation error messageTastypie FormValidation 错误信息
【发布时间】:2014-09-18 15:03:48
【问题描述】:

在我的项目中,我将 TastyPies FormValidation 用于我的 ModelResource。但是,当我尝试通过 AJAX PUT 请求(使用 AngularJS)发送无效数据时,例如:

{ "first_name": "", ... }

我收到回复:

{"customuser": {"first_name": ["[u'This field is required.']"]}}

由于字符串中的方括号和 unicode 前缀,我无法将错误解析到我的表单中。知道如何摆脱它吗?

编辑:原来问题出在 django-angular 模块上。他们的 NgModelFormMixin 导致了这个错误。不过还没有找到解决办法。

【问题讨论】:

    标签: django angularjs tastypie


    【解决方案1】:

    原来 django-angular 不能很好地与 TastyPie 配合使用。经过一些调试,我发现他们的 TupleErrorList 没有正确呈现 ValidationErrors。固定类:

    from django.forms.utils import ErrorList
    from django.core.exceptions import ValidationError
    from djangular.forms.angular_base import TupleErrorList
    
    class FixedTupleErrorList(TupleErrorList, ErrorList):
        def __getitem__(self, i):
            """
            This method was missing in django-angulars TupleErrorList
            (don't know why they didn't inherit from the default django ErrorList)
            """
            error = self.data[i]
            if isinstance(error, ValidationError):
                return list(error)[0]
            return error # originally, there was force_text here, but it forced unicode prefixes around strings
    

    【讨论】:

      猜你喜欢
      • 2015-06-23
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 2017-05-12
      • 2018-07-18
      • 2017-02-19
      • 2011-06-14
      • 2013-05-30
      相关资源
      最近更新 更多