futuremind / drf-friendly-errors Goto Github PK
View Code? Open in Web Editor NEWExtension for Django REST framework error display
License: MIT License
Extension for Django REST framework error display
License: MIT License
like
{
"message": "Validation Failed",
"errors": [
{
"resource": "Issue",
"field": "title",
"code": "missing_field"
}
]
}
It is working fine if it Serializer with the normal fields but if Serializer has nested serializer as its field then errors property throwing datatype exception
Been coming back here on and off for about 1.5 years now and I notice the project hasn't been updated for a while. Its unfortunate given that it adds some nifty features to DRF.
Hope @tomlaszczuk and the rest of the team @FutureMind can be able to find some time to keep the project up to date with Django & DRF releases.
Anyone know of a similar project out there or a fork of this?
To handle custom classes with key word argument error messages, we must ovverride field_map property in serializer class. It's not the best approach. Need better and simpler
Django REST Framework recommends raising a ValidationError
in to_internal_value()
method of a custom field to report a validation error (see DRF > API Guide > Fields > Raising validation errors). The plugin crashes with AttributeError
when I do that. It assumes that the serializer has a method called validate_<field name>
while it does not.
Here's a custom field example taken from DRF docs (linked above). Imports and serializers added by me:
# color_serializers.py
import re
import six
from rest_framework.serializers import Field, Serializer, ValidationError
from rest_framework_friendly_errors.mixins import FriendlyErrorMessagesMixin
class Color(object):
"""
A color represented in the RGB colorspace.
"""
def __init__(self, red, green, blue):
assert(red >= 0 and green >= 0 and blue >= 0)
assert(red < 256 and green < 256 and blue < 256)
self.red, self.green, self.blue = red, green, blue
class ColorField(Field):
"""
Color objects are serialized into 'rgb(#, #, #)' notation.
"""
def to_representation(self, obj):
return "rgb(%d, %d, %d)" % (obj.red, obj.green, obj.blue)
def to_internal_value(self, data):
if not isinstance(data, six.text_type):
msg = 'Incorrect type. Expected a string, but got %s'
raise ValidationError(msg % type(data).__name__)
if not re.match(r'^rgb\([0-9]+,[0-9]+,[0-9]+\)$', data):
raise ValidationError('Incorrect format. Expected `rgb(#,#,#)`.')
data = data.strip('rgb(').rstrip(')')
red, green, blue = [int(col) for col in data.split(',')]
if any([col > 255 or col < 0 for col in (red, green, blue)]):
raise ValidationError('Value out of range. Must be between 0 and 255.')
return Color(red, green, blue)
class ColorSerializer(Serializer):
color = ColorField()
class FriendlyColorSerializer(FriendlyErrorMessagesMixin, Serializer):
color = ColorField()
from .color_serializers import ColorSerializer
color_serializer = ColorSerializer(data = {'color': u'not a color'})
color_serializer.is_valid(raise_exception = True)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/cameel/.virtualenvs/test/lib/python2.7/site-packages/rest_framework/serializers.py", line 222, in is_valid
raise ValidationError(self.errors)
ValidationError: {'color': [u'Incorrect format. Expected `rgb(#,#,#)`.']}
from .color_serializers import FriendlyColorSerializer
friendly_color_serializer = FriendlyColorSerializer(data = {'color': u'not a color'})
friendly_color_serializer.is_valid(raise_exception = True)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/cameel/.virtualenvs/test/lib/python2.7/site-packages/rest_framework/serializers.py", line 222, in is_valid
raise ValidationError(self.errors)
File "/home/cameel/.virtualenvs/test/lib/python2.7/site-packages/rest_framework_friendly_errors/mixins.py", line 23, in errors
pretty_errors = self.build_pretty_errors(ugly_errors)
File "/home/cameel/.virtualenvs/test/lib/python2.7/site-packages/rest_framework_friendly_errors/mixins.py", line 204, in build_pretty_errors
self.get_field_error_entries(errors[error_type], field),
File "/home/cameel/.virtualenvs/test/lib/python2.7/site-packages/rest_framework_friendly_errors/mixins.py", line 178, in get_field_error_entries
return [self.get_field_error_entry(error, field) for error in errors]
File "/home/cameel/.virtualenvs/test/lib/python2.7/site-packages/rest_framework_friendly_errors/mixins.py", line 166, in get_field_error_entry
validator = getattr(self, "validate_%s" % field.field_name)
AttributeError: 'FriendlyColorSerializer' object has no attribute 'validate_color'
Current release supports only Django 1.9 and Django REST 3.3, Although there are many new releases for the same.
Is there any planned development for this amazing library to add support for latest Django and Django REST?
Django 4 doesn't support ugettextlazy. Consider updating your settings.py
file in line 4
.
Replace: from django.utils.translation import ugettext_lazy as _
with: form django.utils.translation import gettext_lazy as _
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.