@@ -72,8 +72,11 @@ def __init__(self, *args, **kwargs):
72
72
default_id_field = primary_key
73
73
meta .id_field = getattr (meta , 'id_field' , default_id_field )
74
74
75
- if not hasattr (meta , 'geo_field' ) or not meta .geo_field :
76
- raise ImproperlyConfigured ("You must define a 'geo_field'." )
75
+ if not hasattr (meta , 'geo_field' ):
76
+ raise ImproperlyConfigured (
77
+ "You must define a 'geo_field'. "
78
+ "Set it to None if there is no geometry."
79
+ )
77
80
78
81
def check_excludes (field_name , field_role ):
79
82
"""make sure the field is not excluded"""
@@ -93,7 +96,9 @@ def add_to_fields(field_name):
93
96
meta .fields += additional_fields
94
97
95
98
check_excludes (meta .geo_field , 'geo_field' )
96
- add_to_fields (meta .geo_field )
99
+
100
+ if meta .geo_field is not None :
101
+ add_to_fields (meta .geo_field )
97
102
98
103
meta .bbox_geo_field = getattr (meta , 'bbox_geo_field' , None )
99
104
if meta .bbox_geo_field :
@@ -118,7 +123,7 @@ def to_representation(self, instance):
118
123
processed_fields = set ()
119
124
120
125
# optional id attribute
121
- if self .Meta .id_field :
126
+ if self .Meta .id_field is not None :
122
127
field = self .fields [self .Meta .id_field ]
123
128
value = field .get_attribute (instance )
124
129
feature ["id" ] = field .to_representation (value )
@@ -128,12 +133,15 @@ def to_representation(self, instance):
128
133
# must be "Feature" according to GeoJSON spec
129
134
feature ["type" ] = "Feature"
130
135
131
- # required geometry attribute
132
- # MUST be present in output according to GeoJSON spec
133
- field = self .fields [self .Meta .geo_field ]
134
- geo_value = field .get_attribute (instance )
135
- feature ["geometry" ] = field .to_representation (geo_value )
136
- processed_fields .add (self .Meta .geo_field )
136
+ # geometry attribute
137
+ # must be present in output according to GeoJSON spec
138
+ if self .Meta .geo_field is not None :
139
+ field = self .fields [self .Meta .geo_field ]
140
+ geo_value = field .get_attribute (instance )
141
+ feature ["geometry" ] = field .to_representation (geo_value )
142
+ processed_fields .add (self .Meta .geo_field )
143
+ else :
144
+ feature ["geometry" ] = None
137
145
138
146
# Bounding Box
139
147
# if auto_bbox feature is enabled
@@ -211,7 +219,7 @@ def unformat_geojson(self, feature):
211
219
"""
212
220
attrs = feature ["properties" ]
213
221
214
- if 'geometry' in feature :
222
+ if 'geometry' in feature and self . Meta . geo_field :
215
223
attrs [self .Meta .geo_field ] = feature ['geometry' ]
216
224
217
225
if self .Meta .id_field and 'id' in feature :
0 commit comments