首页 > 解决方案 > 我想在 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 来获取数据。

预先感谢。

标签: pythonsqldjangopostgresqldjango-queryset

解决方案


推荐阅读