Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KeyError exception on retrieving cached model #363

Closed
mmoallemi99 opened this issue Jul 16, 2020 · 5 comments
Closed

KeyError exception on retrieving cached model #363

mmoallemi99 opened this issue Jul 16, 2020 · 5 comments

Comments

@mmoallemi99
Copy link

mmoallemi99 commented Jul 16, 2020

Hi People,

I have this model called Palette and I'm using Django 2.2 latest patch, Django Cacheops 4.2
and once a week I have this exception breaking my API:

KeyError: 'palette'
File "django/db/models/fields/related_descriptors.py", line 164, in get
rel_obj = self.field.get_cached_value(instance)
File "django/db/models/fields/mixins.py", line 13, in get_cached_value
return instance._state.fields_cache[cache_name]
Palette.DoesNotExist: Palette matching query does not exist.
File "django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "rest_framework/viewsets.py", line 114, in view
return self.dispatch(request, *args, **kwargs)
File "rest_framework/views.py", line 505, in dispatch
response = self.handle_exception(exc)
File "rest_framework/views.py", line 465, in handle_exception
self.raise_uncaught_exception(exc)
File "rest_framework/views.py", line 476, in raise_uncaught_exception
raise exc
File "rest_framework/views.py", line 502, in dispatch
response = handler(request, *args, **kwargs)
File "palettes/views.py", line 190, in list
return super(PaletteViewSet, self).list(request, *args, **kwargs)
File "rest_framework/mixins.py", line 43, in list
return self.get_paginated_response(serializer.data)
File "rest_framework/serializers.py", line 760, in data
ret = super().data
File "rest_framework/serializers.py", line 260, in data
self._data = self.to_representation(self.instance)
File "rest_framework/serializers.py", line 678, in to_representation
self.child.to_representation(item) for item in iterable
File "rest_framework/serializers.py", line 678, in
self.child.to_representation(item) for item in iterable
File "rest_framework/serializers.py", line 529, in to_representation
ret[field.field_name] = field.to_representation(attribute)
File "rest_framework/fields.py", line 1905, in to_representation
return method(value)
File "palettes/serializers.py", line 367, in get_colors
return ColorsUsedInPaletteSerializer(qs, many=True, context=self.context).data[:5]
File "rest_framework/serializers.py", line 760, in data
ret = super().data
File "rest_framework/serializers.py", line 260, in data
self._data = self.to_representation(self.instance)
File "palettes/serializers.py", line 76, in to_representation
return super(PaletteItemListSerializer, self).to_representation(data)
File "rest_framework/serializers.py", line 678, in to_representation
self.child.to_representation(item) for item in iterable
File "rest_framework/serializers.py", line 678, in
self.child.to_representation(item) for item in iterable
File "rest_framework/serializers.py", line 529, in to_representation
ret[field.field_name] = field.to_representation(attribute)
File "rest_framework/fields.py", line 1905, in to_representation
return method(value)
File "cacheops/query.py", line 115, in wrapper
cache_key = prefix + 'as:' + key_func(func, args, kwargs, key_extra)
File "cacheops/utils.py", line 92, in func_cache_key
return md5hex(json.dumps(factors, sort_keys=True, default=obj_key))
File "init.py", line 238, in dumps
**kw).encode(obj)
File "json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "cacheops/utils.py", line 85, in obj_key
return str(obj)
File "rest_framework/serializers.py", line 537, in repr
return representation.serializer_repr(self, indent=1)
File "rest_framework/utils/representation.py", line 71, in serializer_repr
ret = field_repr(serializer, force_many) + ':'
File "rest_framework/utils/representation.py", line 54, in field_repr
arg_string = ', '.join([smart_repr(val) for val in field._args])
File "rest_framework/utils/representation.py", line 54, in
arg_string = ', '.join([smart_repr(val) for val in field._args])
File "rest_framework/utils/representation.py", line 33, in smart_repr
value = repr(value)
File "django/db/models/query.py", line 253, in repr
return '<%s %r>' % (self.class.name, data)
File "django/db/models/base.py", line 519, in repr
return '<%s: %s>' % (self.class.name, self)
File "palettes/models.py", line 140, in str
return f'{self.color} in {self.palette}'
File "django/db/models/fields/related_descriptors.py", line 178, in get
rel_obj = self.get_object(instance)
File "django/db/models/fields/related_descriptors.py", line 145, in get_object
return qs.get(self.field.get_reverse_related_filter(instance))
File "cacheops/query.py", line 359, in get
return qs._no_monkey.get(qs, *args, **kwargs)
File "django/db/models/query.py", line 408, in get
self.model._meta.object_name

And when I apply invalidate_model on Palette model, the issue is fixed.
I would deeply appreciate any help on this.

@Suor
Copy link
Owner

Suor commented Jul 16, 2020

Can you try latest casheops?

@mmoallemi99
Copy link
Author

Updated to django-cacheops 5.0.1

@Suor
Copy link
Owner

Suor commented Jul 17, 2020

The error itself is not in casheops code, judging by your stack trace. Do you use other caching solution with it?

@Suor
Copy link
Owner

Suor commented Jul 17, 2020

If not can you write a test illustrating your issue? The instruction is at the end of README.

@Suor
Copy link
Owner

Suor commented Sep 26, 2020

Looks like a dup of #348 after some though. Bad stack trace conceals it.

@Suor Suor closed this as completed Sep 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants