DRF throws 500 if we raise Django ValidationError from validate_unique

36 views
Skip to first unread message

JD Solanki

unread,
Jul 21, 2020, 7:11:30 AM7/21/20
to Django REST framework
Hi, I have been wondering doesn't DRF convert Django's ValidationError raised from `validate_unique` or from `clean` method of the model?

I have ModelViewSet:
```python
class UpvoteViewSet(ModelViewSet):
    queryset = Upvote.objects.all()
    serializer_class = UpvoteSerializer
```

and I have added `validate_unique` method to model:

```python
    def validate_unique(self, *args, **kwargs):

        super().validate_unique(*args, **kwargs)
        upvote_model = self.__class__

        if upvote_model.objects.filter(upvoted_item=self.upvoted_item, upvoted_user=self.upvoted_user).exists():
            raise ValidationError(['This user already upvoted this item.', ])
```

The problem is `ValidationError` is from Django and if I use DRF's ValidationError then I get an error in Django Admin. So, what's happening. Does DRF handle this??

wenlu han

unread,
Aug 19, 2020, 11:26:03 PM8/19/20
to django-res...@googlegroups.com
HEHE

JD Solanki <jdsola...@gmail.com> 于2020年7月21日周二 下午7:11写道:
--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/873ecf8a-ffd9-459b-b271-d78d05633684o%40googlegroups.com.

Ryoma Han

unread,
Aug 20, 2020, 7:53:42 PM8/20/20
to Django REST framework
As you said,  you get an error in Django Admin, not DRF Admin!
Reply all
Reply to author
Forward
0 new messages