Bird
0
0

This Django code tries to cache a complex object but causes an error:

medium📝 Debug Q14 of 15
Django - Caching
This Django code tries to cache a complex object but causes an error:
from django.core.cache import cache

class MyObject:
    def __init__(self, value):
        self.value = value

obj = MyObject(10)
cache.set('obj', obj, timeout=300)

What is the likely cause of the error?
ATimeout value must be a string, not integer
Bcache.set requires a string value only
CThe object is not serializable for caching
DMyObject class must inherit from Django model
Step-by-Step Solution
Solution:
  1. Step 1: Understand cache storage requirements

    Django cache backends usually require cached data to be serializable (e.g., picklable).
  2. Step 2: Check object serializability

    Custom class instances like MyObject may not be serializable by default, causing errors.
  3. Final Answer:

    The object is not serializable for caching -> Option C
  4. Quick Check:

    Non-serializable objects cause cache errors [OK]
Quick Trick: Cache only serializable data like strings or dicts [OK]
Common Mistakes:
MISTAKES
  • Thinking cache only accepts strings
  • Believing timeout must be string
  • Assuming class must be Django model to cache

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes