[Django] #36451: Issue with GeoFeatureModelSerializer and GeneratedField returning WKT instead of GeoJSON

7 views
Skip to first unread message

Django

unread,
Jun 10, 2025, 12:38:58 PM6/10/25
to django-...@googlegroups.com
#36451: Issue with GeoFeatureModelSerializer and GeneratedField returning WKT
instead of GeoJSON
------------------------+------------------------------------------------
Reporter: QyuBee | Type: Bug
Status: new | Component: Core (Serialization)
Version: 5.1 | Severity: Normal
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------+------------------------------------------------
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 to_representation() in the serializer to
manually convert the WKT to GeoJSON using GEOSGeometry, 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,
--
Ticket URL: <https://code.djangoproject.com/ticket/36451>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jun 10, 2025, 12:43:40 PM6/10/25
to django-...@googlegroups.com
#36451: Issue with GeoFeatureModelSerializer and GeneratedField returning WKT
instead of GeoJSON
-------------------------------------+-------------------------------------
Reporter: QyuBee | Owner: (none)
Type: Bug | Status: new
Component: Core | Version: 5.1
(Serialization) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by QyuBee:

Old description:
New description:
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,

--
--
Ticket URL: <https://code.djangoproject.com/ticket/36451#comment:1>

Django

unread,
Jun 10, 2025, 12:43:59 PM6/10/25
to django-...@googlegroups.com
As a workaround, I’ve overridden field in serializer with GeometryField,
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,

--
--
Ticket URL: <https://code.djangoproject.com/ticket/36451#comment:2>

Django

unread,
Jun 10, 2025, 12:59:49 PM6/10/25
to django-...@googlegroups.com
#36451: Issue with GeoFeatureModelSerializer and GeneratedField returning WKT
instead of GeoJSON
-------------------------------------+-------------------------------------
Reporter: QyuBee | Owner: (none)
Type: Bug | Status: closed
Component: Core | Version: 5.1
(Serialization) |
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* resolution: => invalid
* status: new => closed

Comment:

This sounds like an issue with either djangorestframework's support for
`GeneratedField` in `ModelSerializer` or the more specific GIS
functionality provided by djangorestframework-gis (in its `ready()`
method), not an issue in Django.

> As a workaround, I’ve overridden field in serializer with GeometryField,
but it feels like something that should be handled natively if the output
field is a PointField.

Taking a look at `ModelSerializer.serializer_field_mapping` in DRF, I
don't see any fathoming of `GeneratedField`, so it's probably a missing
feature. You could probably easily subclass that (as well as
GeneratedField, e.g. GeneratedGeometryField) to get what you want. Hope
that helps!
--
Ticket URL: <https://code.djangoproject.com/ticket/36451#comment:3>
Reply all
Reply to author
Forward
0 new messages