查询集:使用 AND 进行左连接

发布于 2024-11-04 03:44:54 字数 1311 浏览 4 评论 0原文

我使用带有 hack 的旧 Django 版本 1.1,它支持 join in extra()。它有效,但现在是改变的时候了。 Django 1.2 使用 RawQuerySet,因此我重写了该解决方案的代码。问题是,RawQuery 不支持过滤器等,我在代码中有很多。 通过 Google 进行挖掘,CaktusGroup我发现我可以使用 query.join()。 这会很棒,但在代码中我有:

LEFT OUTER JOIN "core_rating" ON 
("core_film"."parent_id" = "core_rating"."parent_id" 
AND "core_rating"."user_id" = %i

在 query.join() 中,我已经编写了第一部分 "core_film"."parent_id" = "core_ rating"."parent_id" 但我没有知道如何在 AND 之后添加第二部分。
Django 是否存在任何解决方案,我可以使用自定义 JOIN 而无需重写所有过滤器代码(原始)?

这是我们当前在 extra() 中的代码片段

top_films = top_films.extra(  
    select=dict(guess_rating='core_rating.guess_rating_alg1'),  
    join=['LEFT OUTER JOIN "core_rating" ON ("core_film"."parent_id" = "core_rating"."parent_id" and "core_rating"."user_id" = %i)' % user_id] + extra_join,  
    where=['core_film.parent_id in (select parent_id from core_film EXCEPT select film_id from filmbasket_basketitem where "wishlist" IS NOT NULL and user_id=%i)' % user_id,   
           '( ("core_rating"."type"=1 AND "core_rating"."rating" IS NULL) OR "core_rating"."user_id" IS NULL)',  
           ' "core_rating"."last_displayed" IS NULL'],  
     )

I use old Django version 1.1 with hack, that support join in extra(). It works, but now is time for changes. Django 1.2 use RawQuerySet so I've rewritten my code for that solution. Problem is, that RawQuery doesn't support filters etc. which I have many in code.
Digging through Google, on CaktusGroup I've found, that I could use query.join().
It would be great, but in code I have:

LEFT OUTER JOIN "core_rating" ON 
("core_film"."parent_id" = "core_rating"."parent_id" 
AND "core_rating"."user_id" = %i

In query.join() I've written first part "core_film"."parent_id" = "core_rating"."parent_id" but I don't know how to add the second part after AND.
Does there exist any solution for Django, that I could use custom JOINs without rewritting all the filters code (Raw)?

This is our current fragment of code in extra()

top_films = top_films.extra(  
    select=dict(guess_rating='core_rating.guess_rating_alg1'),  
    join=['LEFT OUTER JOIN "core_rating" ON ("core_film"."parent_id" = "core_rating"."parent_id" and "core_rating"."user_id" = %i)' % user_id] + extra_join,  
    where=['core_film.parent_id in (select parent_id from core_film EXCEPT select film_id from filmbasket_basketitem where "wishlist" IS NOT NULL and user_id=%i)' % user_id,   
           '( ("core_rating"."type"=1 AND "core_rating"."rating" IS NULL) OR "core_rating"."user_id" IS NULL)',  
           ' "core_rating"."last_displayed" IS NULL'],  
     )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月竹挽风 2024-11-11 03:44:54

不幸的是,这里的答案是否定的。

Django ORM 与大多数 Django 一样,遵循这样的理念:简单的事情应该很简单,困难的事情应该成为可能。在这种情况下,您肯定处于“困难的事情”区域,“可能的”解决方案是简单地编写原始查询。肯定存在这样的情况,编写原始查询可能很困难并且感觉有点恶心,但从项目的角度来看,这样的情况太罕见了,无法证明添加此类功能的成本是合理的。

Unfortunately, the answer here is no.

The Django ORM, like most of Django, follows a philosophy that easy things should be easy and hard things should be possible. In this case, you are definitely in the "hard things" area and the "possible" solution is to simply write the raw query. There are definitely situations like this where writing the raw query can be difficult and feels kinda gross, but from the project's perspective situations like this are too rare to justify the cost of adding such functionality.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文