v1 introduced the layer_classes class variable, together with a get_layer_class_kwargs. Previously I was able to directly use the request.query_params and kwargs when filtering the queryset. Unfortunately get_layer_class_kwargs only receives self, but self does not directly have access to request and kwargs of the path. I work around this, by setting some magic variables on the class instance, but this does not feel safe. What is the recommended way to do it in v1?
class FeatureVectorLayer(VectorLayer):
def __init__(self, fields: tuple[str, ...], **kwargs) -> "FeatureVectorLayer":
self.queryset = get_queryset(**kwargs)
self.tile_fields = fields
class TileServerView(MVTAPIView):
view_name = "tileserver"
layer_classes = [FeatureVectorLayer]
def get_layer_class_kwargs(self):
return {"fields": self.request.query_params.get_list("fields"), **self.path_kwargs}
@method_decorator(cache_page(60 * 60 * 24))
def get(self, request: Request, *args, **kwargs) -> HttpResponse:
self.path_kwargs = kwargs # magic instance variable
return super().get(
request=request,
z=kwargs.get("z"),
x=kwargs.get("x"),
y=kwargs.get("y"),
)
Edit: During review I found that self does have a request instance variable, but it was not set in one of our tests using RequestFactory.
v1 introduced the
layer_classesclass variable, together with aget_layer_class_kwargs. Previously I was able to directly use therequest.query_paramsandkwargswhen filtering the queryset. Unfortunatelyget_layer_class_kwargsonly receivesself, butselfdoes not directly have access torequestandkwargsof the path. I work around this, by setting some magic variables on the class instance, but this does not feel safe. What is the recommended way to do it in v1?Edit: During review I found that
selfdoes have arequestinstance variable, but it was not set in one of our tests usingRequestFactory.