Python 限制从SQL查询返回无值
我正在使用OdooERP,我想从GenerateSQL查询的字典列表中限制为不返回任何值Python 限制从SQL查询返回无值,python,sql,postgresql,openerp,odoo-8,Python,Sql,Postgresql,Openerp,Odoo 8,我正在使用OdooERP,我想从GenerateSQL查询的字典列表中限制为不返回任何值 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, (se
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()
上面生成的字典值列表找到了值的描述,并且使用上面的SQL查询得到了“无”结果
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()
我不想要None值,而是想要空字符串或其他空空格空字符串
我应该如何限制None值?您需要使用COALESCE postgresql函数处理空值
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()
感谢Emipro技术公司。。我还从奥多论坛得到了不同的解决方案,这可能对某些人有所帮助