2
我設置Django & Redis項目並嘗試在Redis中存儲django模型對象。如果我使用下面的django.core.cache
,card2被正確設置爲django對象。但是,如果我使用由django_redis提供的原始redis conenction,它將獲得django模型對象的字符串表示形式。在redis中存儲和獲取django模型對象
我設置了兩個鍵如下。如何使用原始redis連接來獲取卡對象本身而不是其字符串repr?我需要它,因爲我想使用mget
,zrange
類似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)