Search code examples
djangoangularjstastypie

Tastypie FormValidation error message


In my project, I use TastyPies FormValidation for my ModelResource. However, when I try to send invalid data via AJAX PUT request (with AngularJS), like:

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

I get the response:

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

I can't parse the error into my form, because of the square brackets and the unicode prefix inside the string. Any idea how to get rid of that?

Edit: Turns out the problem is with the django-angular module. Their NgModelFormMixin causes the bug. Still haven't found the solution though.


Solution

  • Turns out django-angular doesn't play well with TastyPie. After some debugging, I found out that their TupleErrorList didn't correctly render ValidationErrors. Fixed class:

    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