Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion rest_framework_gis/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
from django.contrib.gis.db import models
from django.contrib.gis.db.models.fields import BaseSpatialField
from django.contrib.gis.db.models.functions import GeometryDistance
from django.contrib.gis.gdal.error import GDALException
from django.contrib.gis.geos import Point, Polygon
from django.contrib.gis.geos.error import GEOSException
from django.core.exceptions import ImproperlyConfigured
from django.db.models import Q
from rest_framework.exceptions import ParseError
Expand Down Expand Up @@ -92,13 +94,31 @@ def get_schema_operation_parameters(self, view):
InBBOXFilter = InBBoxFilter


class SafeGeometryField(forms.GeometryField):
Copy link

Copilot AI Dec 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing blank lines before class definition. According to PEP 8, there should be two blank lines before top-level class definitions. There should be two blank lines before this class definition.

Copilot uses AI. Check for mistakes.
"""
GeometryField that converts GEOS/GDAL errors into ValidationError.
"""

def to_python(self, value):
try:
return super().to_python(value)
except (GEOSException, GDALException, ValueError, TypeError):
raise forms.ValidationError("Invalid geometry value.")


class GeometryFilter(django_filters.Filter):
field_class = forms.GeometryField
field_class = SafeGeometryField

def __init__(self, *args, **kwargs):
kwargs.setdefault("widget", forms.TextInput)
super().__init__(*args, **kwargs)

def filter(self, qs, value):
try:
return super().filter(qs, value)
except (GEOSException, GDALException, ValueError, TypeError):
raise forms.ValidationError("Invalid geometry value.")


class GeoFilterSet(django_filters.FilterSet):
GEOFILTER_FOR_DBFIELD_DEFAULTS = {
Expand Down