lookups.py 1.4 KB

12345678910111213141516171819202122232425262728
  1. from django.db.models.lookups import Lookup
  2. from django.db.models.sql.expressions import SQLEvaluator
  3. class GISLookup(Lookup):
  4. def as_sql(self, qn, connection):
  5. from django.contrib.gis.db.models.sql import GeoWhereNode
  6. # We use the same approach as was used by GeoWhereNode. It would
  7. # be a good idea to upgrade GIS to use similar code that is used
  8. # for other lookups.
  9. if isinstance(self.rhs, SQLEvaluator):
  10. # Make sure the F Expression destination field exists, and
  11. # set an `srid` attribute with the same as that of the
  12. # destination.
  13. geo_fld = GeoWhereNode._check_geo_field(self.rhs.opts, self.rhs.expression.name)
  14. if not geo_fld:
  15. raise ValueError('No geographic field found in expression.')
  16. self.rhs.srid = geo_fld.srid
  17. db_type = self.lhs.output_field.db_type(connection=connection)
  18. params = self.lhs.output_field.get_db_prep_lookup(
  19. self.lookup_name, self.rhs, connection=connection)
  20. lhs_sql, lhs_params = self.process_lhs(qn, connection)
  21. # lhs_params not currently supported.
  22. assert not lhs_params
  23. data = (lhs_sql, db_type)
  24. spatial_sql, spatial_params = connection.ops.spatial_lookup_sql(
  25. data, self.lookup_name, self.rhs, self.lhs.output_field, qn)
  26. return spatial_sql, spatial_params + params