Opened 2 months ago

Last modified 5 days 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: yes
Needs tests: yes Patch needs improvement: yes
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 (8)

comment:1 by Md. Saikat Islam, 2 months 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, 2 months ago

Triage Stage: Unreviewed → Accepted

comment:3 by Md. Saikat Islam, 2 months ago

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

comment:4 by Md. Saikat Islam, 2 months 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, 2 months ago

Needs tests: set

comment:6 by Md. Saikat Islam, 2 months ago

Has patch: set

comment:7 by Md. Saikat Islam, 2 months ago

comment:8 by Jacob Walls, 5 days ago

Needs documentation: set
Patch needs improvement: set
Note: See TracTickets for help on using tickets.
Back to Top