将Django 1.8中复杂子查询的结果注释为QuerySet

时间:2015-08-06 15:29:37

标签: mysql sql django django-queryset django-orm

我必须遵循数据库架构:

main_account
- username

invoice
- amount
- currency: 'EUR' or 'USD' or 'GBP'
- username (main_account has many invoices)

我的目标是列出"顶级付款人"用美元。因此,我必须计算一个" total_paid"发票表中每个用户的总和(并考虑货币转换)。我还想通过这个过滤" total_paid"。

我当前的SQL看起来像:

SELECT main_account.username, total_paid
FROM main_account JOIN
(
    SELECT username,
        (SELECT SUM(amount) FROM invoice WHERE main_account.username = invoice.username AND invoice.currency = 'EUR') AS total_eur,
        (SELECT SUM(amount) FROM invoice WHERE main_account.username = invoice.username AND invoice.currency = 'USD') AS total_usd,
        (SELECT SUM(amount) FROM invoice WHERE main_account.username = invoice.username AND invoice.currency = 'GBP') AS total_gbp,
        (SELECT (IFNULL(total_usd,0) + 1.12 * IFNULL(total_eur,0) + 1.41 * IFNULL(total_gbp,0))) AS total_paid
    FROM main_account
) as tbl
ON main_account.username = tbl.username
WHERE total_paid >= 2000
ORDER BY total_paid

我想知道如何使用Django的ORM来实现这一目标。

似乎解决方案有点像:

MainAccount.objects
    .annotate(total_paid=???)
    .filter(total_paid__gte=2000)
    .order_by('total_paid')

一些注意事项:

  • MainAccount.extra(...)。filter(...)。order_by(...)不起作用。在extra中创建的值无法过滤。

  • 我已经尝试过MainAccount.annotate(total_paid = RawSQL(...)),它工作正常,但在添加.filter()时遇到了一个奇怪的错误。出于某种原因,过滤器调用使用SQL参数改变RawSQL对象,然后抛出"not all arguments converted during string formatting"错误。

1 个答案:

答案 0 :(得分:5)

正如BogdiG所说,Django 1.8 Conditional Expressions是解决方案。

的Python / Django的:

MainAccount.objects.annotate(
    total_paid = Sum(
        Case(
            When(invoices__currency='EUR', then=F('invoices__amount') * 1.12),
            When(invoices__currency='USD', then=F('invoices__amount')),
            When(invoices__currency='GBP', then=F('invoices__amount') * 1.41)
        )
    )
).filter(total_paid__gte=2000).order_by('total_paid')

生成类似于:

的SQL
SELECT  main_account.username, 
        SUM(
            CASE 
                WHEN invoice.currency = 'EUR' THEN (invoice.amount * 1.12) 
                WHEN invoice.currency = 'USD' THEN invoice.amount 
                WHEN invoice.currency = 'GBP' THEN (invoice.amount * 1.41) 
                ELSE NULL 
            END
        ) AS total_paid 
FROM main_account 
    INNER JOIN invoice ON ( main_account.username = invoice.username ) 
GROUP BY main_account.username 
HAVING total_paid >= '2000' 
ORDER BY total_paid;