How can I raise custom Exception in validate

1,269 views
Skip to first unread message

Sinan Ülker

unread,
Jun 11, 2017, 9:57:08 PM6/11/17
to Django REST framework
Hello, 

Since serializer.is_valid()  does not catch the Exceptions other than the validationError, how can i raise custom exceptions in validate method and then catch get them with serializer.errors

for example:

class Foo(serializers.Serializer):
    def validate(self, data):
        raise exceptions.AuthenticationFailed("message")
#### then I want to test it like this but serializer.is_valid() line always return error. 
serializer = Foo(some.data)
is_valid = serializer.is_valid()
expected_error = {['message.']}

self.assertFalse(is_valid)
self.assertEqual(serializer.errors, expected_error)


I also could not figure out how to handle it with custom exception handler can you help me about this. I also appreciate if you supply some examples



Jani Tiainen

unread,
Jun 12, 2017, 2:19:48 AM6/12/17
to django-res...@googlegroups.com

Hi,

You need to raise validation error (or subclass of it).

Another option is to move authentication check outside serializer (which might be right thing to do)
and catch that error properly there (or do custom handling).

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

-- 
Jani Tiainen

Sinan Ülker

unread,
Jun 12, 2017, 6:30:49 AM6/12/17
to django-res...@googlegroups.com
Is there any example of custom handling in serializer. I couldn't figure out how to do that. 

To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

-- 
Jani Tiainen

--
You received this message because you are subscribed to a topic in the Google Groups "Django REST framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-rest-framework/8Ai8Vrss0Gg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-rest-framework+unsub...@googlegroups.com.

Jani Tiainen

unread,
Jun 12, 2017, 6:39:46 AM6/12/17
to django-res...@googlegroups.com

Hi,

To me it looks like you're doing wrong solution for your problem.

Serializer is mainly meant to convert data from complex format to python primitives and vice versa.

Validation is to make sure that converted (deserialized) object matches the definition of the serializer.

Now, to me it looks like you want to check is something authenticated. That's not exactly job for serializer,
but job of your view code (one that calls deserialization) after object has been successfully deserialized and validated.

-- You received this message because you are subscribed to the Google Groups "Django REST framework" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
-- 
Jani Tiainen

Sinan Ülker

unread,
Jun 12, 2017, 7:36:48 AM6/12/17
to Django REST framework
Thank you so much. Code is already written so i need to change the way it checks authentication.  That's why I asked about custom handling but as you said it is a design problem. I will take your advices and try to refactor the code. Thanks for the help. 

12 Haziran 2017 Pazartesi 13:39:46 UTC+3 tarihinde Jani Tiainen yazdı:
Reply all
Reply to author
Forward
0 new messages