Why does the first example throw TypeError (can't pickle function objects)
and the second one doesn't, I suppose it's got to do with QuerySet evaluation (Django 1.4)?
def get_or_set_foo_cache():
if not cache.get('foo'):
foo = Foo.objects.annotate(bar_count=Count('bar')).filter(bar_count__gte=1)
print type(foo) # prints <class 'django.db.models.query.QuerySet'>
cache.set('foo', foo, 60 * 15)
return foo
return cache.get('foo')
Example 2
def get_or_set_foo_cache():
if not cache.get('foo'):
foo = Foo.objects.all()
print type(foo) # prints <class 'django.db.models.query.QuerySet'>
cache.set('foo', foo, 60 * 15)
return foo
return cache.get('foo')
If I set foo with list comprehension it works:
foo = [obj for obj in Foo.objects.annotate(bar_count=Count('bar')).filter(bar_count__gte=1)]