【问题标题】:Is There a way of returning a queryset of the first instance of a specific field from a django model有没有办法从 django 模型中返回特定字段的第一个实例的查询集
【发布时间】:2020-08-14 22:45:00
【问题描述】:

假设我有一个名为 Workout 的模型,它的填充如下

我想返回当前用户过滤的每个新日期的第一个实例的查询集。在这种情况下,如果当前登录的用户是 user_id = 1,则查询集将包含 id 为 1,4 和 5 的锻炼对象。有没有办法使用 Workout.objects... 方法来实现这一点? 干杯。

【问题讨论】:

    标签: django django-models django-views


    【解决方案1】:

    您可以在日期字段上使用 distinct 来获得基于日期字段的唯一锻炼。

    Workout.objects.filter(user=user).distinct('date')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-21
      • 2015-07-19
      • 2015-11-19
      • 1970-01-01
      相关资源
      最近更新 更多