python - 设计通知系统数据库的正确方法是什么?
问题描述
例如,我有以下模型
class User(models.Model):
username = ...
avatar = ...
class Article(models.Model):
user = models.ForeignKey(User)
title = ...
content = ...
class Comment(models.Model):
user = models.ForeignKey(User)
# 'Comment' or 'Article'
target_type = models.CharField()
target_id = models.IntegerField()
content = ...
class Like(models.Model):
user = models.ForeignKey(User)
# 'Comment' or 'Article'
target_type = models.CharField()
target_id = models.IntegerField()
和Notification
:
class Notification(models.Model):
actor = models.ForeignKey(User)
receiver = models.ForeignKey(User)
# 'Comment' or 'Like' or '@'
verb = models.CharField()
# Where the notification happens
source_type = models.CharField()
source_id = models.IntegerField()
is_read = ...
time = ...
source_type
指示我需要查找哪个表并且source_id
是id
那个表object
(可能是 aComment
或 aLike
或其他)
我需要序列化notification
如下:
[
{
"actor": {
"username": "Yriuns",
"avatar": null
},
"verb": "Comment",
"source": {
"pk": 542,
"user": {
"username": "Yriuns",
"avatar": null
},
"content": "this is a reply",
"time": "2018.11.30 02:38",
"target": {
"pk": 540,
"user": {
"username": "Someone",
"avatar": null
},
"content": "this is a comment"
}
},
"time": "2018-11-30 02:38:08",
"is_read": false
},
...
]
问题是:我不知道查询数据库(MySQL
)获取source
字段的有效方法。
例如,可能有 10 个通知,其中 5 个是Comment
,其中 3 个是 ,其中Like
2 个是@
。到目前为止,我一一查询它们,这确实很慢。
注意 source
有一个user
字段,它也应该被查询。
- 我怎样才能加快速度?
- 我是否正确设计了数据库?
我试过GenericForeignKey
了,它的效果不是很好,prefetch_related
因为:
- 不同的型号有不同的
ForeignKey
,我只能prefetch_related
说常见的。 - 我不能使用
Prefetch()
对象,因为GenericForeignKey
不支持自定义查询集。
解决方案
我认为Notification
可以是您提供的这三个的基类(在django 模型实用user
程序中具有公共属性). Using
InheritanceManager`,您可以在单个(不是那么优化,但仍然)查询中查询所有通知。
这些类型和 id 看起来也像一个通用的外键候选者。你看感谢解决方案吗?
推荐阅读
- r - Fedora 28 中的 libgfortran.so.4
- bootstrap-4 - Bootstrap4 堆叠卡
- c# - 在 GridView1_RowDataBound 中找不到控件
- c# - 如何加载递归实体而不在根级别加载子实体?
- r - 更改选定列的列名
- android - 即使添加了侦听器,Android FirebaseListAdapter 也无法正常工作
- resharper - 如何在 ReSharper 中刷新“单元测试会话”结果?(粘性失败测试)
- python - 如何在 Flask 中构造使用相同功能的多个路由和渲染模板
- android - Phonegap - 构建失败并出现异常
- symfony4 - Symfony4 安全问题