Opened 5 months ago
Last modified 5 months ago
#36451 closed Bug
Issue with GeoFeatureModelSerializer and GeneratedField returning WKT instead of GeoJSON — at Version 1
| Reported by: | QyuBee | Owned by: | |
|---|---|---|---|
| Component: | Core (Serialization) | Version: | 5.1 |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description (last modified by )
Hi team,
I'm currently using GeoFeatureModelSerializer (from rest_framework_gis) to serialize a model that includes a GeneratedField representing a geographic Point. However, instead of returning a GeoJSON geometry object, the serializer returns a WKT string like this:
{
"id": 483602,
"type": "Feature",
"geometry": "SRID=4326;POINT (-3.826841 48.576644)",
"properties": {
"id_data": "29019_5010_00013",
"libelle": "13 rue Traverse, 29200 Brest"
}
},
Here’s a simplified version of my setup:
point_geo = models.GeneratedField(
output_field=models.PointField(blank=True, null=True),
db_persist=True,
expression=Func(F('lon'), F('lat'), function='ST_MakePoint', output_field=PointField(srid=4326)),
blank=True,
null=True
)
This point_geographique field is computed based on available coordinates (lon, lat or x, y) and works fine at the DB level. However, since it's a GeneratedField, it seems like GeoFeatureModelSerializer does not recognize it as a proper geometry field and falls back to a generic model field, which results in the WKT string in the output.
In serializer, if i print the fields, it returns :
'point_geo': ModelField(allow_null=True, model_field=<django.db.models.fields.generated.GeneratedField: point_geo>, read_only=True)
As a workaround, I’ve overridden field in serializer with PointField, but it feels like something that should be handled natively if the output field is a PointField.
Is there any plan to support GeneratedField-based geometry fields in future versions of DRF-GIS? Or is there a cleaner approach that I might be missing?
Thanks in advance for your help!
Best regards,