Cache persisting despite being disabled by child serializer
@sylvainlehmann reported to me that despite using
class ResponseSerializer(LDPSerializer):
with_cache = False
class ResponseViewSet(LDPViewSet):
serializer_class = ResponseSerializer
The serializer cache was still active, when it should not be. First step is to replicate this in a unit test
Activity
-
Newest first Oldest first
-
Show all activity Show comments only Show history only
- Calum Mackervoy added BUG label
added BUG label
- Author Maintainer
Posting a workaround found by @sylvainlehmann
class ResponseContainerSerializer(ContainerSerializer): @property def with_cache(self): return False class ResponseSerializer(LDPSerializer): with_cache: False class ResponseViewSet(LDPViewSet): serializer_class = ResponseSerializer def build_serializer(self, meta_args, name_prefix): meta_args['list_serializer_class'] = ResponseContainerSerializer return super(ResponseViewSet, self).build_serializer(meta_args, name_prefix)
Collapse replies - Maintainer
Note this seems to append on nested field requests
Here is model definition
class Poll(Model): [removed code] class Meta(Model.Meta): [removed code] nested_fields = ["questions"] rdf_type = 'sib:poll' container_path = 'polls/' class ResponseContainerSerializer(ContainerSerializer): @property def with_cache(self): return False class ResponseSerializer(LDPSerializer): with_cache: False class ResponseViewSet(LDPViewSet): serializer_class = ResponseSerializer def build_serializer(self, meta_args, name_prefix): meta_args['list_serializer_class'] = ResponseContainerSerializer return super(ResponseViewSet, self).build_serializer(meta_args, name_prefix) class ResponsePermissions(LDPPermissions): def get_container_permissions(self, request, view, obj=None): perms = super().get_container_permissions(request, view, obj) poll = Model.resolve_parent(request.path) user_has_responded = request.user.is_authenticated and poll.responses.filter(author=request.user).exists() if user_has_responded and 'add' in perms: perms.remove('add') return perms class Response(Model): created_at = models.DateTimeField(auto_now_add=True, null=True, blank=True) author = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='createdVotesResponses', null=True, blank=True, on_delete=models.SET_NULL) poll = models.ForeignKey(Poll, related_name='responses', on_delete=models.CASCADE) class Meta(Model.Meta): auto_author = 'author' anonymous_perms = [] authenticated_perms = ['add'] owner_perms = ['view'] owner_field = 'author' serializer_fields = ['@id', 'created_at', 'author', 'poll', 'items'] rdf_type = 'sib:poll_response' container_path = 'polls_response/' view_set = ResponseViewSet permission_classes = [ResponsePermissions]
Witout
ResponseContainerSerializer
related code, the method get_container_permissions is only called once, on the first request on/polls/1/responses/
and not on further requests
Please register or sign in to reply