python - 我想在 Django 或 sql 中为每种通知类型获取 10 个通知
问题描述
我有一个Notification
表,其中有多个字段,它也包含notification_type
字段。
目前我正在使用这样的查询来为每种通知类型获取 10 条记录。但在这里我正在调用多个查询。
notify_type = Notify.objects.filter(user__id=1).values_list('type', flat=True).distinct()
for n_type in notify_type:
return Notification.objects.filter(user__id=1, type=n_type).order_by('-id')[:10]
我只想使用一个 SQL 查询或 ORM 来获取数据。
预先感谢。
解决方案
推荐阅读
- javascript - 将默认导出和命名导出与汇总混合
- algorithm - 查找最常出现的对
- ios - 以后可以将 iOS 应用程序添加到仅限 Apple Watch 的应用程序吗?
- python - 错误:** *FileNotFoundError: [Errno 2] 没有这样的文件或目录:- 但文件在那里
- nlp - 尝试训练数据时出现文本错误
- c - POSIX 共享内存和信号量在 Mac OS 10.14.6 和 Ubuntu 18.04 上不起作用
- java - 带有setter getter的Java字符串数组
- abcpdf - 无法呈现 HTML。无法访问网址。FACILITY_INTERNET 中的 COM 错误 800c0019。驱动器无法定位磁盘上的特定区域或磁道
- swift - 无法将数据从 NSKeyedUnarchiver 转换为 Object
- python - 如何在 Airflow 工作人员上执行普通的 Celery 任务