Skip to content

Commit bb22de6

Browse files
allyntauvipy
authored andcommitted
Update the way that to_representation removes already processed (#195)
fields, as per #194
1 parent ed39101 commit bb22de6

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

rest_framework_gis/serializers.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,16 @@ def to_representation(self, instance):
9494
"""
9595
# prepare OrderedDict geojson structure
9696
feature = OrderedDict()
97-
# the list of fields that will be processed by get_properties
98-
# we will remove fields that have been already processed
99-
# to increase performance on large numbers
100-
fields = list(self.fields.values())
97+
98+
# keep track of the fields being processed
99+
processed_fields = set()
101100

102101
# optional id attribute
103102
if self.Meta.id_field:
104103
field = self.fields[self.Meta.id_field]
105104
value = field.get_attribute(instance)
106105
feature["id"] = field.to_representation(value)
107-
fields.remove(field)
106+
processed_fields.add(self.Meta.id_field)
108107

109108
# required type attribute
110109
# must be "Feature" according to GeoJSON spec
@@ -115,7 +114,8 @@ def to_representation(self, instance):
115114
field = self.fields[self.Meta.geo_field]
116115
geo_value = field.get_attribute(instance)
117116
feature["geometry"] = field.to_representation(geo_value)
118-
fields.remove(field)
117+
processed_fields.add(self.Meta.geo_field)
118+
119119
# Bounding Box
120120
# if auto_bbox feature is enabled
121121
# bbox will be determined automatically automatically
@@ -126,7 +126,15 @@ def to_representation(self, instance):
126126
field = self.fields[self.Meta.bbox_geo_field]
127127
value = field.get_attribute(instance)
128128
feature["bbox"] = value.extent if hasattr(value, 'extent') else None
129-
fields.remove(field)
129+
processed_fields.add(self.Meta.bbox_geo_field)
130+
131+
# the list of fields that will be processed by get_properties
132+
# we will remove fields that have been already processed
133+
# to increase performance on large numbers
134+
fields = [
135+
field_value for field_key, field_value in self.fields.items()
136+
if field_key not in processed_fields
137+
]
130138

131139
# GeoJSON properties
132140
feature["properties"] = self.get_properties(instance, fields)

0 commit comments

Comments
 (0)