Opened 4 weeks ago

Last modified 4 weeks ago

#37324 assigned New feature

Cannot assign expressions to spatial fields

Reported by: Jacob Walls Owned by: Md. Saikat Islam
Component: GIS Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

SpatialProxy is too strict in the types it allows to be assigned to spatial fields -- it should allow expressions (if it can be implemented simply enough).

See ​fiddle:

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

class Employee(models.Model):
    home = PointField()
    work = PointField()

def run():
    instance = Employee.objects.create(
        home="POINT(1 1)", work="POINT(2 2)"
    )
    # Work from home.
    instance.work = models.F("home")
    instance.save()
  File "/usr/local/lib/python3.12/site-packages/django/contrib/gis/db/models/proxy.py", line 82, in __set__
    raise TypeError(
TypeError: Cannot set Employee SpatialProxy (POINT) with value of type: <class 'django.db.models.expressions.F'>

Marking new feature, as we should document that we added support.

Change History (7)

comment:1 by Md. Saikat Islam, 4 weeks ago

I was able to reproduce the reported TypeError with F("home") assigned to a PointField. I'll work on a fix to allow expressions to be assigned to spatial fields.

comment:2 by Md. Saikat Islam, 4 weeks ago

Triage Stage: Unreviewed → Accepted

comment:3 by Md. Saikat Islam, 4 weeks ago

Owner: set to Md. Saikat Islam
Status: new → assigned

comment:4 by Md. Saikat Islam, 4 weeks ago

The issue persists with Func, Value, and other Combinable subclasses. So it's not only F(django.db.models.expressions.Func) that's an issue. The fix would be to check whether the value's type is Combinable in __set__ in proxy.py.

comment:5 by Md. Saikat Islam, 4 weeks ago

Needs tests: set

comment:6 by Md. Saikat Islam, 4 weeks ago

Has patch: set

comment:7 by Md. Saikat Islam, 4 weeks ago

Note: See TracTickets for help on using tickets.
Back to Top