我正在寻找一种方法将Django模型对象放入一个列表,然后将其存储在会话中。 我发现它需要序列化才能存储在会话中。 因此,当从会话中读取列表时,我首先将其序列化。
但后来我希望能够像以前一样访问我的初始对象,但事实证明它是一个DeserializedObject。
有谁知道我应该如何处理我的要求? 简而言之,这是我尝试的代码,但没有成功
team1_list = generate_random_playerlist() #generates a list of Player() objects request.session['proposed_team_1'] = serializers.serialize('json', team1_list) #some code inbetween des_list = serializers.deserialize('json', request.session['proposed_team_1']) for player in des_list : print("name of player:"+player.last_name) #failsI am looking for a way to put Django model objects into a list and then store this in the session. I found out that it requires serialization before it can be stored in the session. So when reading the list from the session I first serialize it.
But then I was hoping to be able to access my initial objects as before, but it turns out to be a DeserializedObject.
Does anyone know how I should approach my requirements ? In a nutshell this is the code I was trying, yet unsuccessfully
team1_list = generate_random_playerlist() #generates a list of Player() objects request.session['proposed_team_1'] = serializers.serialize('json', team1_list) #some code inbetween des_list = serializers.deserialize('json', request.session['proposed_team_1']) for player in des_list : print("name of player:"+player.last_name) #fails最满意答案
我不会自己序列化模型实例。 只需将主键写入会话即可轻松完成,无需序列化。 然后,您可以在单个查询中从数据库中检索查询集,而不必担心更改的数据,缺少实例等。
request.session['player_ids'] = list(players.values_list('pk', flat=True)) players = Player.objects.filter(pk__in=request.session.get('player_ids', []))如果您的Player实例尚未保存到数据库,则可能需要按照自己的方式进行操作。 然后通过.object属性从DeserializedObject获取实际的模型实例。
for player in des_list : print("name of player:" + player.object.last_name)请参阅反序列化的文档 。
I would not serialize the model instances themselves. Just write their primary keys to the session which is easy and requires no serialization. Then you can retrieve the queryset from the database in a single query and don't have to worry about changed data, missing instances and the like.
request.session['player_ids'] = list(players.values_list('pk', flat=True)) players = Player.objects.filter(pk__in=request.session.get('player_ids', []))If your Player instances are not yet saved to the database, you might have to go your way. Then you get the actual model instance from the DeserializedObject via the .object attribute.
for player in des_list : print("name of player:" + player.object.last_name)See the docs on deserialization.
如何从会话中存储和读取Django模型对象的Python列表?(how can I store and read a Python list of Django model objects from and to the session?)我正在寻找一种方法将Django模型对象放入一个列表,然后将其存储在会话中。 我发现它需要序列化才能存储在会话中。 因此,当从会话中读取列表时,我首先将其序列化。
但后来我希望能够像以前一样访问我的初始对象,但事实证明它是一个DeserializedObject。
有谁知道我应该如何处理我的要求? 简而言之,这是我尝试的代码,但没有成功
team1_list = generate_random_playerlist() #generates a list of Player() objects request.session['proposed_team_1'] = serializers.serialize('json', team1_list) #some code inbetween des_list = serializers.deserialize('json', request.session['proposed_team_1']) for player in des_list : print("name of player:"+player.last_name) #failsI am looking for a way to put Django model objects into a list and then store this in the session. I found out that it requires serialization before it can be stored in the session. So when reading the list from the session I first serialize it.
But then I was hoping to be able to access my initial objects as before, but it turns out to be a DeserializedObject.
Does anyone know how I should approach my requirements ? In a nutshell this is the code I was trying, yet unsuccessfully
team1_list = generate_random_playerlist() #generates a list of Player() objects request.session['proposed_team_1'] = serializers.serialize('json', team1_list) #some code inbetween des_list = serializers.deserialize('json', request.session['proposed_team_1']) for player in des_list : print("name of player:"+player.last_name) #fails最满意答案
我不会自己序列化模型实例。 只需将主键写入会话即可轻松完成,无需序列化。 然后,您可以在单个查询中从数据库中检索查询集,而不必担心更改的数据,缺少实例等。
request.session['player_ids'] = list(players.values_list('pk', flat=True)) players = Player.objects.filter(pk__in=request.session.get('player_ids', []))如果您的Player实例尚未保存到数据库,则可能需要按照自己的方式进行操作。 然后通过.object属性从DeserializedObject获取实际的模型实例。
for player in des_list : print("name of player:" + player.object.last_name)请参阅反序列化的文档 。
I would not serialize the model instances themselves. Just write their primary keys to the session which is easy and requires no serialization. Then you can retrieve the queryset from the database in a single query and don't have to worry about changed data, missing instances and the like.
request.session['player_ids'] = list(players.values_list('pk', flat=True)) players = Player.objects.filter(pk__in=request.session.get('player_ids', []))If your Player instances are not yet saved to the database, you might have to go your way. Then you get the actual model instance from the DeserializedObject via the .object attribute.
for player in des_list : print("name of player:" + player.object.last_name)See the docs on deserialization.
发布评论