代码之家  ›  专栏  ›  技术社区  ›  Evg

用django orm查询

  •  2
  • Evg  · 技术社区  · 15 年前

    我需要用django orm选择行。我需要类似的查询

    select * from order where (user_from = u and f1 not is null) or (user_to = u and f2 not is null)
    

    Order.objects.filter(user_from = self).exclude(f1 = None)+Order.objects.filter(user_to = self).exclude(f2 = None)
    

    但在orm里没有工会。。orm如何完成这样的任务? (我看到了一种在我的模型中添加一些字段的解决方案,但是在不添加字段的情况下解决它很有意思)

    1 回复  |  直到 15 年前
        1
  •  1
  •   Manoj Govindan    15 年前

    看一看 Q objects . 您可以执行以下操作:

    Order.objects.filter(
        Q(user_from = u, f1__isnull = False) | Q(user_to = u, f2__isnull = False)
    )