Skip to content

Commit

Permalink
Add ValidationError to except in get_object_or_404 for django 1.11
Browse files Browse the repository at this point in the history
  • Loading branch information
njamaleddine committed May 6, 2017
1 parent 69b0ac5 commit cdb8a3c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
3 changes: 2 additions & 1 deletion rest_framework/generics.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
from __future__ import unicode_literals

from django.core.exceptions import ValidationError
from django.db.models.query import QuerySet
from django.http import Http404
from django.shortcuts import get_object_or_404 as _get_object_or_404
Expand All @@ -18,7 +19,7 @@ def get_object_or_404(queryset, *filter_args, **filter_kwargs):
"""
try:
return _get_object_or_404(queryset, *filter_args, **filter_kwargs)
except (TypeError, ValueError):
except (TypeError, ValueError, ValidationError):
raise Http404


Expand Down
20 changes: 19 additions & 1 deletion tests/test_generics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import pytest
from django.db import models
from django.http import Http404
from django.shortcuts import get_object_or_404
from django.test import TestCase
from django.utils import six
Expand All @@ -10,7 +11,8 @@
from rest_framework.response import Response
from rest_framework.test import APIRequestFactory
from tests.models import (
BasicModel, ForeignKeySource, ForeignKeyTarget, RESTFrameworkModel
BasicModel, ForeignKeySource, ForeignKeyTarget, RESTFrameworkModel,
UUIDForeignKeyTarget
)

factory = APIRequestFactory()
Expand Down Expand Up @@ -647,3 +649,19 @@ def destroy(self, request, *args, **kwargs):
view.delete('test request', 'test arg', test_kwarg='test')
assert view.called is True
assert view.call_args == data


class GetObjectOr404Tests(TestCase):
def setUp(self):
super(GetObjectOr404Tests, self).setUp()
self.uuid_object = UUIDForeignKeyTarget.objects.create(name='bar')

def test_get_object_or_404_with_valid_uuid(self):
obj = generics.get_object_or_404(
UUIDForeignKeyTarget, pk=self.uuid_object.pk
)
assert obj == self.uuid_object

def test_get_object_or_404_with_invalid_string_for_uuid(self):
with pytest.raises(Http404):
generics.get_object_or_404(UUIDForeignKeyTarget, pk='not-a-uuid')

0 comments on commit cdb8a3c

Please sign in to comment.