Skip to content

Commit 58ec8d0

Browse files
committed
[fix] attribute error when trying to get bounding box from a method field value
1 parent 624973e commit 58ec8d0

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

rest_framework_gis/serializers.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
LIST_SERIALIZER_KWARGS,
77
ListSerializer,
88
ModelSerializer,
9+
SerializerMethodField,
910
)
1011

1112
from .fields import GeometryField, GeometrySerializerMethodField # noqa
@@ -132,7 +133,12 @@ def to_representation(self, instance):
132133
# MUST be present in output according to GeoJSON spec
133134
field = self.fields[self.Meta.geo_field]
134135
geo_value = field.get_attribute(instance)
135-
feature["geometry"] = field.to_representation(geo_value)
136+
if isinstance(field, SerializerMethodField):
137+
method = getattr(field.parent, field.method_name)
138+
geo_value = method(instance)
139+
feature["geometry"] = field.to_representation(instance)
140+
else:
141+
feature["geometry"] = field.to_representation(geo_value)
136142
processed_fields.add(self.Meta.geo_field)
137143

138144
# Bounding Box
@@ -143,7 +149,11 @@ def to_representation(self, instance):
143149
# otherwise it can be determined via another field
144150
elif self.Meta.bbox_geo_field:
145151
field = self.fields[self.Meta.bbox_geo_field]
146-
value = field.get_attribute(instance)
152+
if isinstance(field, SerializerMethodField):
153+
method = getattr(field.parent, field.method_name)
154+
value = method(instance)
155+
else:
156+
value = field.get_attribute(instance)
147157
feature["bbox"] = value.extent if hasattr(value, 'extent') else None
148158
processed_fields.add(self.Meta.bbox_geo_field)
149159

0 commit comments

Comments
 (0)