[Django] #34629: AttributeError when using filter argument with Collect in GeoDjango Aggregate

39 views
Skip to first unread message

Django

unread,
Jun 3, 2023, 6:43:05 AM6/3/23
to django-...@googlegroups.com
#34629: AttributeError when using filter argument with Collect in GeoDjango
Aggregate
--------------------------------------------------+------------------------
Reporter: Olivier Le Thanh Duong | Owner: nobody
Type: Bug | Status: new
Component: GIS | Version: dev
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 1
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
--------------------------------------------------+------------------------
When using the `django.contrib.gis.db.models.aggregates.Collect` aggregate
function in GeoDjango, a `AttributeError` is raised when a filter argument
is passed. The error is in GeoAggregate so I believe it affect all the
other GeoAggregate: MakeLine, Union, Extend and Extent3D

Reproduction Steps:
1. Define the `Form` and `Submission` models as follows:
{{{#!python
from django.contrib.gis.db.models import PointField
from django.db import models

class Form(models.Model):
name = models.TextField()

class Submission(models.Model):
location = PointField()
name = models.ForeignKey(Form, on_delete=models.CASCADE)
deleted = models.BooleanField(default=False)
}}}
2. Execute the following query to annotate the `submission_center` using
`Collect`, applying a filter to only include undeleted submissions:
{{{#!python
Form.objects.annotate(submission_locations=Collect('submission__location',
filter=Q(deleted=False)))
}}}

Expected Result:
The query should execute successfully, annotating the `submission_center`
field in the `Form` model with the collected locations of undeleted
submissions.

Actual Result:
An `AttributeError` is raised with the following traceback:
{{{
File ~/.virtualenvs/iaso/lib/python3.8/site-
packages/django/contrib/gis/db/models/aggregates.py:44, in
GeoAggregate.resolve_expression(self, query, allow_joins, reuse,
summarize, for_save)
42 c = super().resolve_expression(query, allow_joins, reuse,
summarize, for_save)
43 for expr in c.get_source_expressions():
---> 44 if not hasattr(expr.field, 'geom_type'):
45 raise ValueError('Geospatial aggregates only allowed on
geometry fields.')
46 return c

AttributeError: 'WhereNode' object has no attribute 'field'
}}}

Versions Affected:
This issue can be reproduced on Django versions 3.2.15 and the main branch
(1136aa5005f0ae70fea12796b7e37d6f027b9263 of 3 June). The Git log suggests
that this problem exists in all versions between the reported ones.

I have a the working of a patch and will open a corresponding pull request
in GitHub

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

Django

unread,
Jun 3, 2023, 9:44:47 AM6/3/23
to django-...@googlegroups.com
#34629: AttributeError when using filter argument with Collect in GeoDjango
Aggregate
-------------------------------------+-------------------------------------
Reporter: Olivier Le Thanh | Owner: Olivier
Duong | Le Thanh Duong
Type: Bug | Status: assigned
Component: GIS | Version: dev
Severity: Normal | Resolution:

Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Olivier Le Thanh Duong):

* owner: nobody => Olivier Le Thanh Duong
* status: new => assigned


Comment:

Corresponding Pull request: https://github.com/django/django/pull/16939

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

Django

unread,
Jun 3, 2023, 1:44:00 PM6/3/23
to django-...@googlegroups.com
#34629: AttributeError when using filter argument with Collect in GeoDjango
Aggregate
-------------------------------------+-------------------------------------
Reporter: Olivier Le Thanh | Owner: Olivier
Duong | Le Thanh Duong
Type: Bug | Status: assigned
Component: GIS | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Claude Paroz):

* stage: Unreviewed => Accepted


Comment:

Good catch!

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

Django

unread,
Jun 6, 2023, 4:45:57 AM6/6/23
to django-...@googlegroups.com
#34629: AttributeError when using filter argument with Collect in GeoDjango
Aggregate
-------------------------------------+-------------------------------------
Reporter: Olivier Le Thanh | Owner: Olivier
Duong | Le Thanh Duong
Type: New feature | Status: assigned

Component: GIS | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* type: Bug => New feature


--
Ticket URL: <https://code.djangoproject.com/ticket/34629#comment:3>

Django

unread,
Jun 6, 2023, 5:03:05 AM6/6/23
to django-...@googlegroups.com
#34629: AttributeError when using filter argument with Collect in GeoDjango
Aggregate
-------------------------------------+-------------------------------------
Reporter: Olivier Le Thanh | Owner: Olivier
Duong | Le Thanh Duong
Type: New feature | Status: assigned
Component: GIS | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* needs_better_patch: 0 => 1
* needs_tests: 0 => 1
* needs_docs: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/34629#comment:4>

Django

unread,
Jun 13, 2023, 5:14:52 AM6/13/23
to django-...@googlegroups.com
#34629: AttributeError when using filter argument with Collect in GeoDjango
Aggregate
-------------------------------------+-------------------------------------
Reporter: Olivier Le Thanh | Owner: Olivier
Duong | Le Thanh Duong
Type: New feature | Status: assigned
Component: GIS | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* needs_docs: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/34629#comment:5>

Django

unread,
Jun 16, 2023, 3:33:33 AM6/16/23
to django-...@googlegroups.com
#34629: AttributeError when using filter argument with Collect in GeoDjango
Aggregate
-------------------------------------+-------------------------------------
Reporter: Olivier Le Thanh | Owner: Olivier
Duong | Le Thanh Duong
Type: New feature | Status: assigned
Component: GIS | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* needs_tests: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/34629#comment:6>

Django

unread,
Jun 16, 2023, 6:23:24 AM6/16/23
to django-...@googlegroups.com
#34629: AttributeError when using filter argument with Collect in GeoDjango
Aggregate
-------------------------------------+-------------------------------------
Reporter: Olivier Le Thanh | Owner: Olivier
Duong | Le Thanh Duong
Type: New feature | Status: assigned
Component: GIS | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* needs_better_patch: 1 => 0
* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/34629#comment:7>

Django

unread,
Jun 16, 2023, 1:24:48 PM6/16/23
to django-...@googlegroups.com
#34629: AttributeError when using filter argument with Collect in GeoDjango
Aggregate
-------------------------------------+-------------------------------------
Reporter: Olivier Le Thanh | Owner: Olivier
Duong | Le Thanh Duong
Type: New feature | Status: closed
Component: GIS | Version: dev
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"1b754d638dc5682265b5b4d1fcf5a94d0312113e" 1b754d63]:
{{{
#!CommitTicketReference repository=""
revision="1b754d638dc5682265b5b4d1fcf5a94d0312113e"
Fixed #34629 -- Added filtering support to GIS aggregates.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/34629#comment:9>

Django

unread,
Jun 16, 2023, 1:24:48 PM6/16/23
to django-...@googlegroups.com
#34629: AttributeError when using filter argument with Collect in GeoDjango
Aggregate
-------------------------------------+-------------------------------------
Reporter: Olivier Le Thanh | Owner: Olivier
Duong | Le Thanh Duong
Type: New feature | Status: assigned

Component: GIS | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"c1cff3c4715f633b5123d32227af5104f8585fc7" c1cff3c4]:
{{{
#!CommitTicketReference repository=""
revision="c1cff3c4715f633b5123d32227af5104f8585fc7"
Refs #34629 -- Changed SDOAGGRTYPE wrapping to Func() in GIS aggregates on
Oracle.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/34629#comment:8>

Reply all
Reply to author
Forward
0 new messages