Skip to content

aget_object_or_404 should catch Django's ValidationError, not DRF's #75

@johnnymetz

Description

@johnnymetz

Problem

aget_object_or_404 in adrf/generics.py catches rest_framework.exceptions.ValidationError, but Django's field validation raises django.core.exceptions.ValidationError for invalid field types (e.g., invalid UUID strings). Invalid values aren't converted to Http404 as intended.

Current Code

https://github.com/em1208/adrf/blob/87f599fefbc88a7a750b22a89369f9c3823efc83/adrf/generics.py

from rest_framework.exceptions import ValidationError
...
except (TypeError, ValueError, ValidationError):
    raise Http404

Expected Behavior

Invalid field types (like invalid UUIDs) should raise Http404 instead of letting django.core.exceptions.ValidationError propagate.

Reference

DRF's get_object_or_404 uses Django's ValidationError: https://github.com/encode/django-rest-framework/blob/365d409adb43ebad7d8a42cab2407ec841d8038e/rest_framework/generics.py#L13

Fix

Change the import to:

from django.core.exceptions import ValidationError

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions