{"record":{"id":"078743fb8cf8f300","repo":"django/django","slug":"this-backend-does-not-support-expressions-for-spec","errorCode":null,"errorMessage":"This backend does not support expressions for specifying distance in the dwithin lookup.","messagePattern":"This backend does not support expressions for specifying distance in the dwithin lookup\\.","errorType":"exception","errorClass":"NotSupportedError","httpStatus":null,"severity":"error","filePath":"django/contrib/gis/db/models/lookups.py","lineNumber":345,"sourceCode":"                    self.lhs.output_field, self.rhs_params, self.lookup_name\n                ),\n            )\n        )\n\n\n@BaseSpatialField.register_lookup\nclass DWithinLookup(DistanceLookupBase):\n    lookup_name = \"dwithin\"\n    sql_template = \"%(func)s(%(lhs)s, %(rhs)s, %(value)s)\"\n\n    def process_distance(self, compiler, connection):\n        dist_param = self.rhs_params[0]\n        if (\n            not connection.features.supports_dwithin_distance_expr\n            and hasattr(dist_param, \"resolve_expression\")\n            and not isinstance(dist_param, Distance)\n        ):\n            raise NotSupportedError(\n                \"This backend does not support expressions for specifying \"\n                \"distance in the dwithin lookup.\"\n            )\n        return super().process_distance(compiler, connection)\n\n    def process_rhs(self, compiler, connection):\n        dist_sql, dist_params = self.process_distance(compiler, connection)\n        self.template_params[\"value\"] = dist_sql\n        rhs_sql, params = super().process_rhs(compiler, connection)\n        return rhs_sql, (*params, *dist_params)\n\n\nclass DistanceLookupFromFunction(DistanceLookupBase):\n    def as_sql(self, compiler, connection):\n        spheroid = (\n            len(self.rhs_params) == 2 and self.rhs_params[-1] == \"spheroid\"\n        ) or None\n        distance_expr = connection.ops.distance_expr_for_lookup(","sourceCodeStart":327,"sourceCodeEnd":363,"githubUrl":"https://github.com/django/django/blob/ae25a40be07e8a749edf526df37c93e59d4a22c9/django/contrib/gis/db/models/lookups.py#L327-L363","documentation":"Raised as NotSupportedError by DWithinLookup.process_distance when the active database backend does not expose supports_dwithin_distance_expr and the supplied distance argument is an expression (has resolve_expression) that is not a Distance instance. SQLite/SpatiaLite and some older backends cannot compile an arbitrary expression into the third argument of ST_DWithin.","triggerScenarios":"On SpatiaLite, Model.objects.filter(geom__dwithin=(pt, F('radius'))) or an annotated expression. The features.supports_dwithin_distance_expr flag is False, dist_param is an Expression, and it is not a Distance, so lookups.py:345 raises.","commonSituations":"Developing locally on SQLite/SpatiaLite and deploying to PostGIS; passing an F() object or aggregate where a Distance measurement is expected.","solutions":["Pass a concrete Distance(...) or numeric literal instead of an expression.","If you need per-row distances, switch to a backend with supports_dwithin_distance_expr=True (PostGIS).","Precompute the distance value in Python and pass it as D(m=value)."],"exampleFix":"// before\nQS.filter(geom__dwithin=(pt, F('radius')))  # on SpatiaLite\n// after\nQS.filter(geom__dwithin=(pt, D(m=obj.radius)))","handlingStrategy":"fallback","validationCode":"from django.db.models import Expression\nfrom django.contrib.gis.measure import Distance\ndef coerce_dwithin_distance(backend_features, dist):\n    if not backend_features.supports_dwithin_distance_expr and isinstance(dist, Expression) and not isinstance(dist, Distance):\n        raise NotSupportedError('Resolve expression to a Distance before dwithin on this backend')\n    return dist","typeGuard":"def is_dwithin_safe(backend_features, dist):\n    return backend_features.supports_dwithin_distance_expr or isinstance(dist, Distance) or not hasattr(dist, 'resolve_expression')","tryCatchPattern":"from django.db import NotSupportedError\ntry:\n    QS.filter(geom__dwithin=(pt, expr))\nexcept NotSupportedError as e:\n    if 'dwithin' in str(e):\n        QS.filter(geom__dwithin=(pt, D(m=resolved_value)))","preventionTips":["Check connection.features.supports_dwithin_distance_expr before passing expressions.","Keep a PostGIS target for production if you need expression-based distance queries."],"tags":["gis","dwithin","backend-compat","spatialite"],"analyzedSha":"ae25a40be07e8a749edf526df37c93e59d4a22c9","analyzedAt":"2026-08-06T21:46:51.801Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}