djangodocs.org

766 sections in Django dev Search all versions →

Q() objects

Reference QuerySet API reference Query-related tools

classQ[source] A Q() object represents an SQL condition that can be used in database-related operations. It’s similar to how an F() object represents the value of a model field or annotation. They make it possible to define and reuse conditions.

Spatial Lookups

Reference GIS QuerySet API Reference

The spatial lookups in this section are available for GeometryField and RasterField. For an introduction, see the spatial lookups introduction. For an overview of what lookups are compatible with a particular spatial backend, refer to the spatial lookup compatibility table.

Lookups with rasters

Reference GIS QuerySet API Reference Spatial Lookups

All examples in the reference below are given for geometry fields and inputs, but the lookups can be used the same way with rasters on both sides.

bbcontains

Reference GIS QuerySet API Reference Spatial Lookups

Availability: PostGIS, MariaDB, MySQL, SpatiaLite, PGRaster (Native) Tests if the geometry or raster field’s bounding box completely contains the lookup geometry’s bounding box.

bboverlaps

Reference GIS QuerySet API Reference Spatial Lookups

Availability: PostGIS, MariaDB, MySQL, SpatiaLite, PGRaster (Native) Tests if the geometry field’s bounding box overlaps the lookup geometry’s bounding box.

contained

Reference GIS QuerySet API Reference Spatial Lookups

Availability: PostGIS, MariaDB, MySQL, SpatiaLite, PGRaster (Native) Tests if the geometry field’s bounding box is completely contained by the lookup geometry’s bounding box.

contains

Reference GIS QuerySet API Reference Spatial Lookups

Availability: PostGIS, Oracle, MariaDB, MySQL, SpatiaLite, PGRaster (Bilateral) Tests if the geometry field spatially contains the lookup geometry.

contains_properly

Reference GIS QuerySet API Reference Spatial Lookups

Availability: PostGIS, PGRaster (Bilateral) Returns true if the lookup geometry intersects the interior of the geometry field, but not the boundary (or exterior).

coveredby

Reference GIS QuerySet API Reference Spatial Lookups

Availability: PostGIS, Oracle, MariaDB, MySQL, PGRaster (Bilateral), SpatiaLite Tests if no point in the geometry field is outside the lookup geometry.

covers

Reference GIS QuerySet API Reference Spatial Lookups

Availability: PostGIS, Oracle, MySQL, PGRaster (Bilateral), SpatiaLite Tests if no point in the lookup geometry is outside the geometry field.

crosses

Reference GIS QuerySet API Reference Spatial Lookups

Availability: PostGIS, MariaDB, MySQL, SpatiaLite, PGRaster (Conversion) Tests if the geometry field spatially crosses the lookup geometry.

disjoint

Reference GIS QuerySet API Reference Spatial Lookups

Availability: PostGIS, Oracle, MariaDB, MySQL, SpatiaLite, PGRaster (Bilateral) Tests if the geometry field is spatially disjoint from the lookup geometry.

equals

Reference GIS QuerySet API Reference Spatial Lookups

Availability: PostGIS, Oracle, MariaDB, MySQL, SpatiaLite, PGRaster (Conversion) Tests if the geometry field is spatially equal to the lookup geometry.

exact, same_as

Reference GIS QuerySet API Reference Spatial Lookups

Availability: PostGIS, Oracle, MariaDB, MySQL, SpatiaLite, PGRaster (Bilateral) Tests if the geometry field is “equal” to the lookup geometry. On Oracle, MySQL, and SpatiaLite, it tests spatial equality, while on PostGIS it tests equality of bounding boxes.

intersects

Reference GIS QuerySet API Reference Spatial Lookups

Availability: PostGIS, Oracle, MariaDB, MySQL, SpatiaLite, PGRaster (Bilateral) Tests if the geometry field spatially intersects the lookup geometry.

isempty

Reference GIS QuerySet API Reference Spatial Lookups

Availability: PostGIS, SpatiaLite Tests if the geometry is empty. Example: Changed in Django 6.1: SpatiaLite support was added.

isvalid

Reference GIS QuerySet API Reference Spatial Lookups

Availability: MariaDB, MySQL, PostGIS, Oracle, SpatiaLite Tests if the geometry is valid. Example: | Backend | SQL Equivalent | | --- | --- | | MariaDB, MySQL, PostGIS, SpatiaLite | ST_IsValid(poly) | | Oracle | SDO_GEOM.VALIDATE_GEOMETRY_WITH_CONTEXT(poly, 0.05) = 'TRUE' |

geom_type

Reference GIS QuerySet API Reference Spatial Lookups

Availability: PostGIS, Oracle 23c+, MariaDB, MySQL, SpatiaLite Returns the geometry type of the geometry field.

num_dimensions

Reference GIS QuerySet API Reference Spatial Lookups

New in Django 6.1. Availability: PostGIS, SpatiaLite Returns the number of dimensions used by the geometry. Example: | Backend | SQL Equivalent | | --- | --- | | PostGIS, SpatiaLite | ST_NDims(geom) |

overlaps

Reference GIS QuerySet API Reference Spatial Lookups

Availability: PostGIS, Oracle, MariaDB, MySQL, SpatiaLite, PGRaster (Bilateral) Tests if the geometry field spatially overlaps the lookup geometry.