#28896: GeoDjango PointField fails to generate query if filtering on a NULL 
value
-------------------------------------+-------------------------------------
               Reporter:  William    |          Owner:  nobody
  Li                                 |
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  2.0
  layer (models, ORM)                |       Keywords:  QuerySet.extra
               Severity:  Normal     |  PointField GeoDjango
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 Environment:
 * Python 3.6.3
 * postgres 9.6.6
 * Django 2.0

 Note: This was working under Django 1.11.7.

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

 class Restaurant(models.Model):
   location = models.PointField(blank=True, null=True, db_index=True)
 }}}


 {{{
 # tests.py
 from django.test import TestCase
 from restaurants.models import Restaurant


 class RestaurantTestCase(TestCase):
     def test_no_location(self):
         # Failing query
         Restaurant.objects.filter(location=None)
 }}}


 {{{
 $ python manage.py testCreating test database for alias 'default'...
 System check identified no issues (0 silenced).
 E
 ======================================================================
 ERROR: test_no_location (restaurants.tests.RestaurantTestCase)
 ----------------------------------------------------------------------
 Traceback (most recent call last):
   File "/home/wli/projects/point-field-bug/restaurants/tests.py", line 7,
 in test_no_location
     Restaurant.objects.filter(location=None)
   File "/home/wli/.envs/point-field-bug/lib/python3.6/site-
 packages/django/db/models/manager.py", line 82, in manager_method
     return getattr(self.get_queryset(), name)(*args, **kwargs)
   File "/home/wli/.envs/point-field-bug/lib/python3.6/site-
 packages/django/db/models/query.py", line 836, in filter
     return self._filter_or_exclude(False, *args, **kwargs)
   File "/home/wli/.envs/point-field-bug/lib/python3.6/site-
 packages/django/db/models/query.py", line 854, in _filter_or_exclude
     clone.query.add_q(Q(*args, **kwargs))
   File "/home/wli/.envs/point-field-bug/lib/python3.6/site-
 packages/django/db/models/sql/query.py", line 1252, in add_q
     clause, _ = self._add_q(q_object, self.used_aliases)
   File "/home/wli/.envs/point-field-bug/lib/python3.6/site-
 packages/django/db/models/sql/query.py", line 1276, in _add_q
     split_subq=split_subq,
   File "/home/wli/.envs/point-field-bug/lib/python3.6/site-
 packages/django/db/models/sql/query.py", line 1214, in build_filter
     condition = self.build_lookup(lookups, col, value)
   File "/home/wli/.envs/point-field-bug/lib/python3.6/site-
 packages/django/db/models/sql/query.py", line 1084, in build_lookup
     lookup = lookup_class(lhs, rhs)
   File "/home/wli/.envs/point-field-bug/lib/python3.6/site-
 packages/django/contrib/gis/db/models/lookups.py", line 23, in __init__
     super().__init__(lhs, rhs)
   File "/home/wli/.envs/point-field-bug/lib/python3.6/site-
 packages/django/db/models/lookups.py", line 18, in __init__
     self.rhs = self.get_prep_lookup()
   File "/home/wli/.envs/point-field-bug/lib/python3.6/site-
 packages/django/db/models/lookups.py", line 68, in get_prep_lookup
     return self.lhs.output_field.get_prep_value(self.rhs)
   File "/home/wli/.envs/point-field-bug/lib/python3.6/site-
 packages/django/contrib/gis/db/models/fields.py", line 188, in
 get_prep_value
     raise ValueError('Cannot use object with type %s for a spatial lookup
 parameter.' % type(obj).__name__)
 ValueError: Cannot use object with type NoneType for a spatial lookup
 parameter.

 ----------------------------------------------------------------------
 Ran 1 test in 0.005s

 FAILED (errors=1)
 Destroying test database for alias 'default'...

 }}}

 Workaround:
 {{{
 Restaurant.objects.extra(where=['location IS NULL'])
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/28896>
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 django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/046.04078b331cacd86eb5fe0ffd3d0f74c0%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to