gpt4 book ai didi

python - 有没有办法在 Django 中序列化多个对象?

转载 作者:行者123 更新时间:2023-12-04 08:58:13 26 4
gpt4 key购买 nike

在 Django 项目中,我想实现一个简单的聊天框。
版本
Django 3.0.8 版
模型.py

class DirectMessageClass(models.Model):  
sender = models.ForeignKey(User, on_delete=models.CASCADE,
related_name='from_this')
receiver = models.ForeignKey(User, on_delete=models.CASCADE,
related_name='to_this')
timestamp = models.DateTimeField(auto_now=True)
content = models.CharField(max_length=200)
read = models.BooleanField(default=False)

def serialize(self):
return {
"id": self.id,
"sender": self.sender.username,
"sendername":self.sender.profile.firstname,
"senderimage":self.sender.profile.image.url,
"receiver": self.receiver.username,
"content": self.content,
"timestamp":self.timestamp.strftime("%m/%d/%Y, %H:%M:%S"), # to format date
"read":self.read,
}
Views.py
def loadbox(request):
user = request.user
# Return messages in reverse chronologial order
messages = DirectMessageClass.objects.filter(receiver=user).order_by("-timestamp").all()
return JsonResponse([message.serialize() for message in messages], safe=False)
我成功获取消息并使用此方法将其转换为/loadbox 路径中的 JSON 对象。稍后,我获取这些数据以显示消息并且它起作用了。
我的/loadbox 路径示例
[{"id": 1, "sender": "ekvatorcizgisi", "sendername": "Asli", 
"receiver": "bilge", "content": "hi", "timestamp": "09/02/2020, 08:22:26", "read": true}]


但是,因为我希望它显示为对话框。我还需要在这个 JSON 对象中添加回复消息。
在 views.py 我尝试
def loadbox(request):
user = request.user
# Return messages in reverse chronologial order
messages = DirectMessageClass.objects.filter(receiver=user).order_by("-timestamp").all()
# return replies sent by the current user
replies = DirectMessageClass.objects.filter(sender=user).order_by("-timestamp").all()
return JsonResponse([message.serialize() for message in messages], safe=False)
我不确定如何在/loadbox 路径中返回多个 Json 响应
return JsonResponse([message.serialize() for message in messages,reply.serialize() for reply in replies], safe=False)
显然给出了语法错误,(SyntaxError:无效语法)我也尝试将其全部放入列表中,但无法使其工作。
我的理想输出是
 [messages: {"id": 1, "sender": "ekvatorcizgisi", "sendername": "Asli", 
"receiver": "bilge", "content": "hi", "timestamp": "09/02/2020, 08:22:26", "read": true}
replies:{"id": 3, "sender": "bilged", "sendername": "bilge",
"receiver": "Asli", "content": "hi there", "timestamp": "09/02/2020, 08:22:26", "read": false}]
也许我会为回复消息做另一条路径来解决这个问题,但我想知道有没有更好的方法来做到这一点?

最佳答案

使用 list 整理 技术

def loadbox(request):
# rest of your code
return JsonResponse(
[
*[message.serialize() for message in messages],
*[reply.serialize() for reply in replies]
]
,
safe=False
)
简单演示:
In [1]: foo = [1,2,3]                                                                                                                                                                                              

In [2]: bar = [5,6,7]

In [3]: foo_bar = [*foo,*bar]

In [4]: foo_bar
Out[4]: [1, 2, 3, 5, 6, 7]

关于python - 有没有办法在 Django 中序列化多个对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63702221/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com