2010-08-01 96 views
1

我有一個queryset的extra()方法的問題。Django模型過濾器()和額外的()

所以,我找回我的對象​​有:

invoices = Invoice.objects.select_related().filter(quantity__gt=0,begin__gte=values['start_day'],end__lte=values['end_day']) 

所以它的工作原理,我有我的發票。 之後,我使用其他時間過濾器():

invoices = invoices.filter(max__gte=duration) 

它也能工作。 但是,之後,我需要因爲我的請求,使用額外的(),所以我有:

cond = 'discount="YES" AND priceeuro*(%d%%fixe)<=%d' 

invoices = invoices.extra(where=[cond],params=[duration,price]) 

那麼,它的工作原理,但我的發票變量包含多個元素之前。 這就像兩個過濾器()沒有使用。

如果你知道爲什麼,

謝謝。

編輯:

這是與查詢相關的SQL:

WHERE 
("invoice"."product_id" IN (
    SELECT U0."id" 
    FROM "product" U0 
    WHERE U0."accommodation_id" IN (
     SELECT U0."id" 
     FROM "accommodation" U0 
     WHERE U0."resort_id" IN (
      SELECT U0."id" 
      FROM "resort" U0 
      WHERE U0."area_id" IN (
       SELECT U0."id" 
       FROM "area" U0 
       WHERE U0."country_id" = 9 
)))) 
AND "invoice"."quantity" > 0 
AND "invoice"."end" <= 2010-12-31 
AND "invoice"."begin" >= 2010-12-01 
AND fixe % 7 = 0 
AND (discount="YES" AND pricediscountincludedeuro*(7% fixe)<=250)OR(discount="NO" AND  priceeuro*(7% fixe)<=250)) 

回答

1

轉儲從查詢中設置對象的SQL:

print invoices.query 

如果你的錯誤的原因從查看生成的SQL並不明顯,更新您的問題併發布SQL以供我們查看。基於看到SQL

我質疑最後一行在你的SQL(重新格式化)

編輯1:

... 
AND (discount="YES" AND pricediscountincludedeuro*(7% fixe)<=250) 
OR (discount="NO" AND priceeuro*(7% fixe)<=250) 
) 

像你想這兩個在我看來, '折扣'檢查包裹在另一組圓括號中以形成自己的邏輯檢查:

... 
AND (
    (discount="YES" AND pricediscountincludedeuro*(7% fixe)<=250) 
    OR 
    (discount="NO" AND priceeuro*(7% fixe)<=250) 
) 
) 

沒有明確的分組,OR將與其他「折扣」檢查無關地進行比較,並且會導致它包含您已經在上述謂詞中排除的內容。