Skip to content

Key error when annotate() for child model applied to QuerySet #1

@epicbagel

Description

@epicbagel

When adding annotate to a QuerySet that is to be cached, it throws a Key Error exception (see below - CommentAttachment has a foreign key point to a Comment model). It works find if counting within the same model, but not with a related model (e.g. through the foreign key)

Note: The error only occurs the first time the query is run.

Comment.objects.cache().filter(id = 1).annotate(Count('commentattachment'))
Traceback (most recent call last):
File "", line 1, in
File "/usr/lib/pymodules/python2.6/django/db/models/query.py", line 68, in repr
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "/usr/lib/pymodules/python2.6/django/db/models/query.py", line 83, in len
self._result_cache.extend(list(self._iter))
File "/usr/lib/python2.6/dist-packages/cachebot/queryset.py", line 434, in iterator
for obj in CacheBot(self):
File "/usr/lib/python2.6/dist-packages/cachebot/queryset.py", line 65, in iter
self.cache_results(results)
File "/usr/lib/python2.6/dist-packages/cachebot/queryset.py", line 107, in cache_results
invalidation_dict.update(dict([(key, self.result_key) for key in self.get_invalidation_keys(results)]))
File "/usr/lib/python2.6/dist-packages/cachebot/queryset.py", line 159, in get_invalidation_keys
related_fields = self.queryset._related_fields
File "/usr/lib/python2.6/dist-packages/cachebot/queryset.py", line 204, in _related_fields
for attname, model_class in self._get_related_models(self.model):
File "/usr/lib/python2.6/dist-packages/cachebot/queryset.py", line 227, in _get_related_models
for join_attname, model_klass in self._get_related_models(model_class):
File "/usr/lib/python2.6/dist-packages/cachebot/queryset.py", line 217, in _get_related_models
related_models.add((rev_reversemapping[attname], related.model))
KeyError: 'update_set'
Comment.objects.cache().filter(id = 1).annotate(Count('commentattachment'))
[]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions