Skip to Content
Menu
This question has been flagged
1 Reply
4107 Views

Anybody have idea about how the query used in python file??

i was  pasted the one of our base module query, if you can please explain me whats the use of this query, and how its works in odoo database.( i founded it from purchase ->report -> purchase_report.py file)


code:-


def _select(self):

select_str = """

SELECT min(l.id) as id,

l.product_id as product_id,

t.uom_id as product_uom,

sum(l.product_uom_qty / u.factor * u2.factor) as product_uom_qty,

sum(l.product_uom_qty * l.price_unit * (100.0-l.discount) / 100.0) as price_total,

count(*) as nbr,

s.date_order as date,

s.date_confirm as date_confirm,

s.partner_id as partner_id,

s.user_id as user_id,

s.company_id as company_id,

extract(epoch from avg(date_trunc('day',s.date_confirm)-date_trunc('day',s.create_date)))/(24*60*60)::decimal(16,2) as delay,

l.state,

t.categ_id as categ_id,

s.pricelist_id as pricelist_id,

s.project_id as analytic_account_id,

s.section_id as section_id

"""

return select_str

def _from(self):

from_str = """

sale_order_line l

join sale_order s on (l.order_id=s.id)

left join product_product p on (l.product_id=p.id)

left join product_template t on (p.product_tmpl_id=t.id)

left join product_uom u on (u.id=l.product_uom)

left join product_uom u2 on (u2.id=t.uom_id)

"""

return from_str

def _group_by(self):

group_by_str = """

GROUP BY l.product_id,

l.order_id,

t.uom_id,

t.categ_id,

s.date_order,

s.date_confirm,

s.partner_id,

s.user_id,

s.company_id,

l.state,

s.pricelist_id,

s.project_id,

s.section_id

"""

return group_by_str

def init(self, cr):

# self._table = sale_report

tools.drop_view_if_exists(cr, self._table)

cr.execute("""CREATE or REPLACE VIEW %s as (

%s

FROM ( %s )

%s

)""" % (self._table, self._select(), self._from(), self._group_by()))

Avatar
Discard
Best Answer

Logicious,

Here a simple string construction is being done from the data returned from different variables and methods[self._table, self._select(), self._from(), self._group_by()] in the __init__() method of your class(defined at the last).

in __init__() method, at

cr.execute("""CREATE or REPLACE VIEW %s as (

%s

FROM ( %s )

%s

)""" % (self._table, self._select(), self._from(), self._group_by()))

'%s' are bieng replaced by the strings returned from (self._table, self._select(), self._from(), self._group_by()) respectively......

and then the query formed will be executed under "cr.execute()"

here they are running a join query on tables

[

sale_order_line l

join sale_order s on (l.order_id=s.id)

left join product_product p on (l.product_id=p.id)

left join product_template t on (p.product_tmpl_id=t.id)

left join product_uom u on (u.id=l.product_uom)

left join product_uom u2 on (u2.id=t.uom_id)

] (under _from() method)

to get data

Hope it helps,,,    

Avatar
Discard
Related Posts Replies Views Activity
0
Dec 21
1486
2
Feb 19
8423
14
Dec 17
99075
0
Sep 17
1867
1
Mar 15
3841