如何将SQL查询函数转换为django查询集

时间:2018-02-23 07:27:12

标签: sql django python-3.x django-models django-rest-framework

我从.sql文件中获得以下示例查询,如何将其转换为django queryset

这是我到目前为止所做的:

class AdjustmentView(generics.ListCreateAPIView):
    queryset = Adjustment.objects.all()
    serializer_class = AdjustmentSerializer

    def get_adjustment(self):
        Adjustment.objects.filter(employee_month_id=1,adjustment_type=2)
        .values('exchange_rate','amount').aggregate(Sum('amount', field="exchange_rate*amount") )

只是一个示例查询集即可。

CREATE OR REPLACE FUNCTION getAdjustment(int, int, int) RETURNS float AS $$
DECLARE
    adjustment float;
BEGIN

    IF ($3 = 1) THEN
        SELECT SUM(exchange_rate * amount) INTO adjustment
        FROM Employee_Adjustments
        WHERE (Employee_Month_ID = $1) AND (adjustment_type = $2);
    ELSIF ($3 = 2) THEN
        SELECT SUM(exchange_rate * amount) INTO adjustment
        FROM Employee_Adjustments
        WHERE (Employee_Month_ID = $1) AND (adjustment_type = $2) AND (In_payroll = true) AND (Visible = true);
    ELSIF ($3 = 3) THEN
        SELECT SUM(exchange_rate * amount) INTO adjustment
        FROM Employee_Adjustments
        WHERE (Employee_Month_ID = $1) AND (adjustment_type = $2) AND (In_Tax = true);

..............................

0 个答案:

没有答案