【发布时间】:2022-01-24 23:07:11
【问题描述】:
我目前正在尝试为聊天应用程序设计数据库架构。
我对存储每条消息的内容的字段类型有些困惑。
这是聊天消息的部分完成的数据库架构:
...
...
...
# types of chat messages available
TEXT = 'text'
IMAGE = 'image'
...
MESSAGE_TYPE = [
(TEXT, _('Chat message type : Text')),
(IMAGE, _('Chat message type : Image')),
....
]
# User class represents a user of the application
class ChatMessage(models.Model):
"""
Class for storing chat messages between `Users`
"""
# type of the message
message_type = models.CharField(choices=MESSAGE_TYPE, max_length=50, null=False)
# user who created the text message
sender = models.ForeignKey(User,related_name='sender', on_delete=models.CASCADE, null=False)
# user who is supposed to receive the message
recipient = models.ForeignKey(User,related_name='recipient', on_delete=models.CASCADE, null=False)
# timestamp at which the message was created
created_at = models.DateTimeField(default=timezone.now)
# whether the recipient has seen the message
seen = models.BooleanField()
# content of the chat message
content = .....
我计划使用编码器/解码器实用程序来解释 content 使用 message_type 字段的不同类型的聊天消息。但我很难指定适合执行此操作的字段。
这个数据库设计是否足以执行这项任务?我应该求助于其他模式吗?我正在使用 PostgreSQL。
我们将不胜感激。
感谢阅读。
【问题讨论】:
-
你可以有一个通用的外键和每个消息类型的模型?
标签: python django django-models database-design django-orm