Skip to content

Commit cdf2686

Browse files
committed
add tests + README
1 parent 6eabd9d commit cdf2686

File tree

5 files changed

+35
-5
lines changed

5 files changed

+35
-5
lines changed

README.rst

+3
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,9 @@ to be serialized as the "geometry". For example:
250250
# as with a ModelSerializer.
251251
fields = ('id', 'address', 'city', 'state')
252252
253+
If your model is geometry-less, you can set ``geo_field`` to ``None``
254+
and a null geometry will be produced.
255+
253256
Using GeometrySerializerMethodField as "geo_field"
254257
##################################################
255258

tests/django_restframework_gis_tests/models.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ class BaseModel(models.Model):
2020
name = models.CharField(max_length=32)
2121
slug = models.SlugField(max_length=128, unique=True, blank=True)
2222
timestamp = models.DateTimeField(null=True, blank=True)
23-
geometry = models.GeometryField()
2423

2524
class Meta:
2625
abstract = True
@@ -45,18 +44,18 @@ def save(self, *args, **kwargs):
4544

4645

4746
class Location(BaseModel):
48-
pass
47+
geometry = models.GeometryField()
4948

5049

51-
class LocatedFile(BaseModel):
50+
class LocatedFile(Location):
5251
file = models.FileField(upload_to='located_files', blank=True, null=True)
5352

5453

55-
class BoxedLocation(BaseModel):
54+
class BoxedLocation(Location):
5655
bbox_geometry = models.PolygonField()
5756

5857

59-
class Nullable(BaseModel):
58+
class Nullable(Location):
6059
geometry = models.GeometryField(blank=True, null=True)
6160

6261

tests/django_restframework_gis_tests/serializers.py

+6
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,12 @@ class Meta:
185185
fields = ['name', 'slug', 'id']
186186

187187

188+
class NoGeoFeatureMethodSerializer(gis_serializers.GeoFeatureModelSerializer):
189+
class Meta:
190+
model = Location
191+
geo_field = None
192+
fields = ['name', 'slug', 'id']
193+
188194
class PointSerializer(gis_serializers.GeoFeatureModelSerializer):
189195
class Meta:
190196
model = PointModel

tests/django_restframework_gis_tests/tests.py

+14
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,20 @@ def test_geometry_serializer_method_field_none(self):
634634
self.assertEqual(response.data['properties']['name'], 'None value')
635635
self.assertEqual(response.data['geometry'], None)
636636

637+
def test_geometry_serializer_method_field_nogeo(self):
638+
location = Location.objects.create(
639+
name='No geometry value'
640+
)
641+
location_loaded = Location.objects.get(pk=location.id)
642+
self.assertEqual(
643+
location_loaded.name, "No geometry value"
644+
)
645+
url = reverse('api_geojson_location_details_nogeo', args=[location.id])
646+
response = self.client.generic('GET', url, content_type='application/json')
647+
self.assertEqual(response.status_code, 200)
648+
self.assertEqual(response.data['properties']['name'], 'No geometry value')
649+
self.assertEqual(response.data['geometry'], None)
650+
637651
def test_nullable_empty_geometry(self):
638652
empty = Nullable(name='empty', geometry='POINT EMPTY')
639653
empty.full_clean()

tests/django_restframework_gis_tests/views.py

+8
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
LocationGeoFeatureWritableIdSerializer,
2525
LocationGeoSerializer,
2626
NoneGeoFeatureMethodSerializer,
27+
NoGeoFeatureMethodSerializer,
2728
PaginatedLocationGeoSerializer,
2829
PolygonModelSerializer,
2930
)
@@ -166,6 +167,13 @@ class GeojsonLocationDetailsNone(generics.RetrieveUpdateDestroyAPIView):
166167

167168
geojson_location_details_none = GeojsonLocationDetailsNone.as_view()
168169

170+
class GeojsonLocationDetailsNoGeo(generics.RetrieveUpdateDestroyAPIView):
171+
model = Location
172+
serializer_class = NoGeoFeatureMethodSerializer
173+
queryset = Location.objects.all()
174+
175+
176+
geojson_location_details_nogeo = GeojsonLocationDetailsNoGeo.as_view()
169177

170178
class GeojsonLocationSlugDetails(generics.RetrieveUpdateDestroyAPIView):
171179
model = Location

0 commit comments

Comments
 (0)