submarcos/django-vectortiles

Manually defining vector_tile_queryset hits the database before the vector tile request

StefanBrand opened this issue · 0 comments

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.

def get_vector_tile_queryset(self):
return self.vector_tile_queryset if self.vector_tile_queryset else self.get_queryset()

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