Skip to content

Commit 73556a6

Browse files
authored
Merge pull request #353 from datamade/feature/serializer
Add doc about serializing with DRF outside of DRF
2 parents 68cce3f + 5514928 commit 73556a6

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ _In alphabetical order and including links to external repository-based document
2929
- [Forms](django/forms.md)
3030
- [Translation](django/translation.md)
3131
- [Wagtail](django/wagtail/)
32+
- [Serializing GeoJSON data](django/serializing-data.md)
3233
- [Docker](docker/)
3334
- [Docker for local development](docker/local-development.md)
3435
- [Templates for containerizing your application](docker/templates/)

django/serializing-data.md

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Using Django Rest Framework serializers for data serialization
2+
Django Rest Framework can be useful for serializing data beyond the context of an API endpoint. We find it helpful whenever we're serializing json for use in a React component, like when you need to pass GeoJSON to a map or your data objects have nested relationships.
3+
4+
[Django Rest Framework's documentation](https://www.django-rest-framework.org/api-guide/serializers/) is the best place to learn about the serializer classes, but this document shows how we use this pattern within the DataMade stack.
5+
6+
## Use cases
7+
- Serializing GeoJSON or JSON data for use in a Django template or React component that's [baked into the Django template](/django/django-react-integration.md)
8+
- Serializing a queryset with nested relationships
9+
- This helps with database efficiency because you can leverage `prefetch_related` and then serialize all of the related data in a cleaner way. [This blog post is a good resource to learn about that](https://hakibenita.com/django-rest-framework-slow).
10+
11+
12+
## Examples:
13+
In the IL NWSS project, we serialized GeoJSON data and linked needed data that is related to the model. I can't share the private repo link since not everybody has access, so these are the relevant changes we made:
14+
15+
### models.py
16+
```python
17+
from django.contrib.gis.db import models'
18+
19+
20+
class SewershedArea(models.Model):
21+
"""Simplified version of the geo model"""
22+
treatment_plant = ParentalKey(
23+
WastewaterTreatmentPlant,
24+
related_name='sewershed_area',
25+
on_delete=models.CASCADE,
26+
primary_key=True,
27+
)
28+
boundary = models.GeometryField(blank=True, null=True, srid=3435, dim=2)
29+
```
30+
31+
### serializers.py
32+
```python
33+
from rest_framework_gis.serializers import (
34+
GeoFeatureModelSerializer,
35+
GeometrySerializerMethodField,
36+
)
37+
38+
39+
class SewershedAreaGeoSerializer(GeoFeatureModelSerializer):
40+
class Meta:
41+
model = SewershedArea
42+
fields = ('pk', 'boundary', 'plant_name', 'plant_url', 'plant_city', 'site_id')
43+
geo_field = 'boundary'
44+
45+
boundary = GeometrySerializerMethodField()
46+
47+
plant_name = serializers.SerializerMethodField()
48+
plant_url = serializers.SerializerMethodField()
49+
plant_city = serializers.SerializerMethodField()
50+
site_id = serializers.SerializerMethodField()
51+
52+
def get_boundary(self, obj):
53+
return obj.boundary.transform(4326, clone=True)
54+
55+
def get_plant_name(self, obj):
56+
return obj.treatment_plant.name
57+
58+
def get_plant_url(self, obj):
59+
return obj.treatment_plant.get_absolute_url()
60+
61+
def get_plant_city(self, obj):
62+
return obj.treatment_plant.city
63+
64+
def get_site_id(self, obj):
65+
return obj.treatment_plant.site_id
66+
```
67+
68+
### Use the data
69+
You can use the serialized data in at least two ways:
70+
1. Serialize the GeoJSON data in your view and pass it into the context, so that you can access that data within the template and pass it into your React component as props (as described in [this how-to documentation](/django/django-react-integration.md#set-react-props-and-root-element-on-the-window-object))
71+
72+
2. Setup an API endpoint with Django Rest Framework

0 commit comments

Comments
 (0)