I setup Django & Redis project and try to store django model object in Redis. If I use django.core.cache
as below, card2 is properly set to django object. However, if I use raw redis conenction provided by django_redis, it gets string representation of django model object.
I set both keys as below. How can I use raw redis connection to get card object itself instead of its string repr? I need it because I'd like to use mget
, zrange
like methods of redis.py.
from django.core.cache import cache
from django_redis import get_redis_connection
con = get_redis_connection("default")
card = Card.objects.all()[0]
key = "card_" + str(card.id)
con.delete(key)
cache.delete(key)
con.set(key, card)
cache.set(key, card)
card1 = con.get(key)
card2 = cache.get(key)
You have to convert your response from redis to your python object manually.
Hint: use pickle, here are examples: 1, 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With