Good day to all. There is a question:

from django_redis import get_redis_connection redis = get_redis_connection("default") ls = [{'foo': 15, 'bar': 16}, {'foo': 78, 'bar': 0}, {'foo': 49, 'bar': 7}, {'foo': 1, 'bar': 5}] for item in ls: #TODO: append to redis ... 

How to add such a structure in redis element by element? To keep the result as:

 'some_key': [{'foo': 15, 'bar': 16}, {'foo': 78, 'bar': 0}, {'foo': 49, 'bar': 7}, {'foo': 1, 'bar': 5}] 

    2 answers 2

    As it turned out, you can simply write json. I did not think right away.

     import json from django_redis import get_redis_connection redis = get_redis_connection("default") ls = [{'foo': 15, 'bar': 16}, {'foo': 78, 'bar': 0}, {'foo': 49, 'bar': 7}, {'foo': 1, 'bar': 5}] for item in ls: redis.lpush('some_key', json.dumps(item)) 

      No need for element:

       redis.set('some_key', ls) 
      • I understand all this, but in my case it is necessary to record one element at a time - Igor Lisenko February