#34838: GeoDjango database functions incompatible with GeneratedField
-------------------------------------+-------------------------------------
               Reporter:  Paolo      |          Owner:  nobody
  Melchiorre                         |
                   Type:  Bug        |         Status:  new
              Component:  GIS        |        Version:  dev
               Severity:  Release    |       Keywords:  field, database,
  blocker                            |  generated, gis, feodjango
           Triage Stage:             |      Has patch:  1
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 GeoDjango model functions raise an incompatibility error when invoked on
 generated fields.

 **Steps**

 Steps to reproduce the error.

 **Model**


 {{{
 from django.contrib.gis.db import models

 class Area(models.Model):
     polygon = models.PolygonField()
     centroid = models.GeneratedField(
         db_persist=True,
         expression=models.functions.Centroid("polygon"),
         output_field=models.PointField(),
     )
 }}}

 **Query**


 {{{
 >>> from django.contrib.gis.geos import Polygon
 >>> Area.objects.create(polygon=Polygon(((0,0), (2,0), (2,2), (0,2),
 (0,0))))
 >>> Area.objects.values_list(models.functions.AsWKT("centroid"),
 models.functions.AsWKT("polygon"))
 }}}

 **Traceback**


 {{{
 Traceback (most recent call last):
   File "<console>", line 1, in <module>
   File "/home/paulox/Projects/django/django/db/models/manager.py", line
 87, in manager_method
     return getattr(self.get_queryset(), name)(*args, **kwargs)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/home/paulox/Projects/django/django/db/models/query.py", line
 1629, in annotate
     return self._annotate(args, kwargs, select=True)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/home/paulox/Projects/django/django/db/models/query.py", line
 1677, in _annotate
     clone.query.add_annotation(
   File "/home/paulox/Projects/django/django/db/models/sql/query.py", line
 1185, in add_annotation
     annotation = annotation.resolve_expression(self, allow_joins=True,
 reuse=None)
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File
 "/home/paulox/Projects/django/django/contrib/gis/db/models/functions.py",
 line 80, in resolve_expression
     raise TypeError(
 TypeError: AsWKT function requires a GeometryField in position 1, got
 GeneratedField.
 }}}

 **Patch**


 {{{
 diff --git a/django/contrib/gis/db/models/functions.py
 b/django/contrib/gis/db/models/functions.py
 index 19da355d28..90ca87a051 100644
 --- a/django/contrib/gis/db/models/functions.py
 +++ b/django/contrib/gis/db/models/functions.py
 @@ -76,6 +76,8 @@ class GeoFuncMixin:
          source_fields = res.get_source_fields()
          for pos in self.geom_param_pos:
              field = source_fields[pos]
 +            if field.generated:
 +                field = field.output_field
              if not isinstance(field, GeometryField):
                  raise TypeError(
                      "%s function requires a GeometryField in position %s,
 got %s."
 @@ -86,7 +88,7 @@ class GeoFuncMixin:
                      )
                  )

 -        base_srid = res.geo_field.srid
 +        base_srid = res.geo_field.srid if not res.geo_field.generated
 else res.geo_field.output_field.srid
          for pos in self.geom_param_pos[1:]:
              expr = res.source_expressions[pos]
              expr_srid = expr.output_field.srid
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34838>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018a8fca3bc2-6b51cc77-557e-4d05-ba81-ed71c661ab6e-000000%40eu-central-1.amazonses.com.

Reply via email to