diff --git a/django/contrib/gis/db/models/functions.py b/django/contrib/gis/db/models/functions.py index 06b44e7b9f..8a4d54aac1 100644 --- a/django/contrib/gis/db/models/functions.py +++ b/django/contrib/gis/db/models/functions.py @@ -382,9 +382,10 @@ class NumPoints(GeoFunc): output_field_class = IntegerField arity = 1 - def as_sqlite(self, compiler, connection): + def as_sql(self, compiler, connection): if self.source_expressions[self.geom_param_pos].output_field.geom_type != 'LINESTRING': - raise TypeError("SpatiaLite NumPoints can only operate on LineString content") + if not connection.features.supports_num_points_poly: + raise TypeError('NumPoints can only operate on LineString content on this database.') return super(NumPoints, self).as_sql(compiler, connection) diff --git a/tests/gis_tests/geoapp/test_functions.py b/tests/gis_tests/geoapp/test_functions.py index 023a6a087f..bbf1fd1144 100644 --- a/tests/gis_tests/geoapp/test_functions.py +++ b/tests/gis_tests/geoapp/test_functions.py @@ -304,11 +304,14 @@ class GISFunctionsTests(TestCase): Track.objects.create(name='Foo', line=LineString(coords)) qs = Track.objects.annotate(num_points=functions.NumPoints('line')) self.assertEqual(qs.first().num_points, 2) - if spatialite or mysql: - # SpatiaLite and MySQL can only count points on LineStrings + mpoly_qs = Country.objects.annotate(num_points=functions.NumPoints('mpoly')) + if not connection.features.supports_num_points_poly: + msg = 'NumPoints can only operate on LineString content on this database.' + with self.assertRaisesMessage(TypeError, msg): + list(mpoly_qs) return - for c in Country.objects.annotate(num_points=functions.NumPoints('mpoly')): + for c in mpoly_qs: self.assertEqual(c.mpoly.num_points, c.num_points) if not oracle: