Opened 15 years ago
Closed 11 years ago
#11599 closed Bug (fixed)
Field exceptions are silently passed when slicing geoqueryset
Reported by: | Jani Tiainen | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
When using GeoDjango exception happens when populating model from database using get().
x = MyModel.objects.get(pk=1)
Above works as expected, correct exception is thrown
But:
qs = MyModel.objects.filter(pk=1) qs[0]
Now IndexError exception is thrown instead of exception that was expected (in my particular case, there was invalid geometry).
Returned list from queryset itself is empty, no error thrown at all.
Change History (10)
comment:1 by , 15 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
comment:2 by , 15 years ago
Resolution: | invalid |
---|---|
Status: | closed → reopened |
>> x = MyModel.objects.get(pk=1) >> TypeError: cannot set MyModel GeometryProxy with value of type: <class 'django.contrib.gis.geos.collections.MultiLineString'>
But
qs = MyModel.objects.filter(pk=1)
using qs.Count()
with returns 1, but qs[0]
returns IndexError. len(qs)
raises TypeError again.
So Count(), len and slicing are not consistent.
comment:4 by , 15 years ago
Yes, slicing geoqueryset fails with IndexError when query result contains geometry field of incorrect type (e.g when model is defined with LineString but database contains MultiLineString).
comment:5 by , 15 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:6 by , 14 years ago
Severity: | → Normal |
---|---|
Type: | → Bug |
comment:9 by , 12 years ago
Status: | reopened → new |
---|
comment:10 by , 11 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
I believe this is part of the more general "exceptions in queryset iteration are swallowed, empty list is returned" problem that has been resolved in master.
This isn't incorrect behaviour. The second example returns a
Queryset
. Always. It happens that the queryset is empty in your case. When you index into the queryset using a single index like this, it is treated the same as indexing into a list. You are asking for a non-existent element, so anIndexError
is raised.