V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
coolair
V2EX  ›  问与答

请教一个 Django ORM 查询问题

  •  
  •   coolair · Dec 30, 2020 · 512 views
    This topic created in 1949 days ago, the information mentioned may be changed or developed.
    query = User.objects.filter(state=2).values('dept_id', 'state').annotate(max_date=Max('created'))
    query = assessment_set.values('state')
    for item in query.all():
        print(item)
    
    # 输出结果如下:
    {'state': 1}
    {'state': 2}
    {'state': 2}
    {'state': 1}
    
    query = query.annotate(count=Count('state'))
    for item in query.all():
        print(item)
    
    # 输出结果如下:
    {'state': 1, 'count': 3}  # 为什么这里不是 2 ?
    {'state': 2, 'count': 2}
    

    统计部门最新用户的状态数据,为什么第二此 query 中的 count 不是基于前面的 state 来的而是所有的 state (未过滤最大日期的)?

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   953 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 23:36 · PVG 07:36 · LAX 16:36 · JFK 19:36
    ♥ Do have faith in what you're doing.