2016-05-02 10 views
2

私はOdoo ERPを使用しています。SQLクエリの生成からDictionary of Listの値を返さないように制限したいと思います。SQLクエリからNone値を返すことを制限する

form_data = self.read(cr, uid, ids, ['start_date','end_date'], context=context)[0] 
sql = "select i.date_invoice, i.number, (select name from res_partner where id=i.partner_id) as partner,i.currency_id, (select name from res_currency where id=i.currency_id) as currency, (select description from account_tax where name=t.name), t.amount, t.base, (t.amount+t.base) as total from account_invoice i, account_invoice_tax t where t.invoice_id = i.id and i.state = 'open' and i.type = 'out_invoice' and i.date_invoice >= '%s' and i.date_invoice <= '%s' order by t.name"%(form_data['start_date'],form_data['end_date']) 

cr.execute(sql) 
data = cr.dictfetchall() 

生成された結果:辞書値の生成されたリストの上に

data=[ 
    {'currency_id': 38, 
    'description': u'GST 7%', 
    'number': u'SAJ/2015/0021', 
    'date_invoice': '2015-05-05', 
    'currency': u'SGD', 
    'amount': 283.08, 
    'base': **None**, 
    'partner': u'partner1', 
    'total': None}, 

    {'currency_id': 38, 
    'description': **None**, 
    'number': u'SAJ/2015/0021', 
    'date_invoice': '2015-05-05', 
    'currency': u'SGD', 
    'amount': 283.08, 
    'base': 4044.0, 
    'partner': u'partner1', 
    'total': 4327.08}, 

    {'currency_id': 38, 
    'description': **None**, 
    'number': u'SAJ/2015/0020', 
    'date_invoice': '2015-05-04', 
    'currency': u'SGD', 
    'amount': 0.0, 
    'base': 3550.0, 
    'partner': u'EAST MARINE PTE LTD', 
    'total': 3550.0}, 

    {'currency_id': 38, 
    'description': **None**, 
    'number': u'SAJ/2015/0021', 
    'date_invoice': '2015-05-05', 
    'currency': u'SGD', 
    'amount': 0.0, 
    'base': 500.0, 
    'partner': u'partner1', 
    'total': 500.0}, 

    {'currency_id': 38, 
    'description': **None**, 
    'number': u'SAJ/2015/0023', 
    'date_invoice': '2015-05-05', 
    'currency': u'SGD', 
    'amount': 0.0, 
    'base': 11100.0, 
    'partner': u'partner2', 
    'total': 11100.0} 

] 

は値の説明キーはSQLクエリの上に使用してなし結果を得ました。

私はしたくないですが、値の代わりに、なしです。空白文字列または空白文字(空文字列)を空にしたいです。

[なし]の値を制限するにはどうすればよいですか?

答えて

6

のCOALESCE postgresql関数を使用すると、NULL値を処理する必要があります。

form_data = self.read(cr, uid, ids, ['start_date','end_date'], context=context)[0] 

sql = """ 
    select 
     i.date_invoice, 
     i.number, 
     (select name from res_partner where id=i.partner_id) as partner, 
     i.currency_id, 
     (select name from res_currency where id=i.currency_id) as currency, 
     COALESCE((select description from account_tax where name=t.name), '') as description, 
     t.amount, 
     t.base, 
     COALESCE((t.amount+t.base),0) as total 
from 
    account_invoice i, account_invoice_tax t 
where 
    t.invoice_id = i.id and 
    i.state = 'open' and 
    i.type = 'out_invoice' and 
    i.date_invoice >= '%s' 
    and i.date_invoice <= '%s' 
order by t.name 
"""%(form_data['start_date'],form_data['end_date']) 

cr.execute(sql) 
data = cr.dictfetchall() 
+0

おかげEmiproテクノロジーズ..私はまたodooフォーラムから私の解決策を取得するための別の方法を持ってhttps://www.odoo.com/forum/help-1/question/restrict-to-return-none- value-from-the-sql-query-101452それはあるものに役立つかもしれません –

関連する問題