【问题标题】:combining get_absolute_url into a raw SQL statement in django将 get_absolute_url 组合成 django 中的原始 SQL 语句
【发布时间】:2013-08-26 20:43:23
【问题描述】:

我正在开发一个 django 应用程序,它使用几个复杂的查询来返回排名、比​​率和其他复杂的输出。我关注了一些来自网络的示例,这些示例帮助我找到了放置它并从返回的查询集中检索数据的最佳方式

然而,我想找到一种将自定义详细信息注入给定记录的方法,例如,在我的例子中,我试图将 get_absolute_url() 值关联到返回的记录集中

下面是一个返回最常用兴趣的例子,这个查询总是返回一个有限的查询集,有没有办法用模型的 get_absolute_url() 值扩展返回的字典?

def most_used_interests(self, limit_by=10):
    cursor = connection.cursor()
    cursor.execute("""
            SELECT
                i.name,
                i.name_ar,
                i.name_en,
                ij.interest_id,
                SUM (ij.C) item_count
            FROM
                (
                    SELECT
                        C .interest_id,
                        COUNT (b. ID) C
                    FROM
                        bargain_bargain b,
                        bargain_bargain_bargain_target C
                    WHERE
                        b. ID = C .bargain_id
                    GROUP BY
                        C .interest_id
                    UNION
                        SELECT
                            x.interest_id,
                            COUNT (P . ID) C
                        FROM
                            promotion_promotion P,
                            promotion_promotion_promo_target x
                        WHERE
                            x.promotion_id = P . ID
                        GROUP BY
                            x.interest_id
                ) ij, list_interest i
            WHERE i.id=ij.interest_id
            GROUP BY
                ij.interest_id,
                i.name,
                i.name_ar,
                i.name_en
            ORDER BY
                item_count DESC
            LIMIT %s
    """, [limit_by, ])
    desc = cursor.description
    if cursor.rowcount:
        return [
            dict(zip([col[0] for col in desc], row))
            for row in cursor.fetchall()
        ]
    return None

【问题讨论】:

  • 您的意思不是在压缩之前获取和扩展列?
  • 是的,尝试添加 get_absolute_url,因为它们分配给返回的字典

标签: sql django postgresql


【解决方案1】:

我找到了一种更简单的方法来处理这种情况,对于面临类似问题的其他人,我将我的查询包装为始终返回对象,然后使用 objects.raw 作为返回的查询

def most_used_interests(self, limit_by=10):
    return self.raw("""
            SELECT
                *
            FROM
                list_interest l,
                (
                    SELECT
                        ij.interest_id,
                        SUM (ij. C) item_count
                    FROM
                        (
                            SELECT
                                C .interest_id,
                                COUNT (b. ID) C
                            FROM
                                bargain_bargain b,
                                bargain_bargain_bargain_target C
                            WHERE
                                b. ID = C .bargain_id
                            GROUP BY
                                C .interest_id
                            UNION
                                SELECT
                                    x.interest_id,
                                    COUNT (P . ID) C
                                FROM
                                    promotion_promotion P,
                                    promotion_promotion_promo_target x
                                WHERE
                                    x.promotion_id = P . ID
                                GROUP BY
                                    x.interest_id
                        ) ij
                    GROUP BY
                        ij.interest_id
                    LIMIT %s
                ) tl
            WHERE
                l. ID = tl.interest_id
            ORDER BY
                item_count DESC
    """, [limit_by, ]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-11
    • 2014-01-07
    • 2021-12-05
    • 1970-01-01
    • 2017-03-22
    • 2020-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多