submarcos / django-vectortiles

Mapbox VectorTiles for django, with PostGIS or Python
https://django-vectortiles.readthedocs.io
MIT License
42 stars 11 forks source link

Manually defining vector_tile_queryset hits the database before the vector tile request #9

Closed StefanBrand closed 3 years ago

StefanBrand commented 3 years ago

I created a custom View like this:

class PostGISFeatureViewWithManualVectorTileQuerySet(PostgisMVTView, DetailView):
    vector_tile_layer_name = "features"
    vector_tile_fields = ('name', )

    def get(self, request, *args, **kwargs):
        self.vector_tile_queryset = Feature.objects.filter(date="2020-07-07")

        return BaseVectorTileView.get(self, request=request, z=kwargs.get('z'), x=kwargs.get('x'), y=kwargs.get('y'))

Then I tested the number of queries like this:

class VectorTileTestCase(VectorTileBaseTest):
    def test_num_queries_equals_one(self):
        self.maxDiff = None
        with self.assertNumQueries(1):
            self.client.get(
                reverse(
                    'feature-postgis-with-manual-vector-tile-queryset',
                    args=(0, 0, 0)
                )
            )

This test fails because if self.vector_tile_queryset evaluates any QuerySet. This is not desired.

https://github.com/submarcos/django-vectortiles/blob/fb2ad8ca8ea9c4384a5325599c95f363ce9e89f9/vectortiles/mixins.py#L21-L22

My colleague (Thank you!) proposed a fix, that I will create a PR for.