使用django orm进行查询

使用django orm进行查询,django,orm,models,Django,Orm,Models,我需要用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) 但o

我需要用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如何完成这样的任务? (我看到了一种在模型中添加一些字段的解决方案,但在不添加字段的情况下解决它很有意思)

看一看。您可以执行以下操作:

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

警告:这是未经测试的代码。查看实际生成的SQL查询并验证这确实是您所需要的,这将是一个好主意。

它得到异常:不允许在字段“to\u url\u placed\u update”上加入。查找类型是否拼错了“is_null”?我第一次看到它是因为使用了uuu is_unull=False您舒尔django orm中的这个功能?我错了<代码>为空应该是
isnull
。更新答案。你能发布
订单
型号的源代码吗?