Unable to disable cache for custom serializer since 2.1.11
On djangoldp-poll, a custom view is defined to get total vote on a survey, defined by poll option.
class TotalVotes(LDPViewSet):
'''view to GET the total counts of votes selecting a particular option'''
serializer_class = PollOptionSerializer
def _get_poll_or_404(self):
pk = self.kwargs['pk']
try:
return Poll.objects.get(pk=pk)
except Poll.DoesNotExist:
raise Http404('could not get a Poll with this ID!')
def get_serializer_context(self):
poll = self._get_poll_or_404()
votes = poll.votes.all()
context = super().get_serializer_context()
context.update({'votes_queryset': votes})
return context
def get_queryset(self, *args, **kwargs):
poll = self._get_poll_or_404()
return poll.pollOptions.all()
def get_serializer_class(self):
# NOTE: this is required because currently DjangoLDP overrides the serializer_class during __init__
# https://git.startinblox.com/djangoldp-packages/djangoldp/issues/241
return PollOptionSerializer
and
class PollOptionSerializer(LDPSerializer):
total_votes = serializers.SerializerMethodField()
with_cache = False
class Meta:
model = PollOption
fields = ['urlid','name', 'total_votes']
def get_total_votes(self, obj):
votes_queryset = self.context.get("votes_queryset")
return votes_queryset.filter(chosenOption=obj).count()
Since new cache update, this endpoint is not updated after each vote, leading to an invalid total.
I found a workaround disabling cache globaly, or adding the following line on top of get_queryset
method
# force cache invalidation
GLOBAL_SERIALIZER_CACHE.invalidate('djangoldp_polls.PollOption')
As there is a working workaround this is not a problem for me anymore, the maybe this could append in other context ?
ping @calummackervoy as asked