This question has been flagged
3 Replies
3146 Views

Hi,  I'm using  Odoo  14  and  I want to add price_unit from purchase_order_line to pivot and graph view of purchase.report. This will involve inheriting pivot and graph  of purchase.report model . 


Here is the error I'm getting .

error

psycopg2.errors.UndefinedColumn: column purchase_report.price_unit does not exist
LINE 3: ..._order_line" AS "purchase_report__price_unit" ON ("purchase_...
My Model
class PurchaseReport(models.Model):
_inherit = "purchase.report"

price_unit = fields.Many2one('purchase.order.line', string='Unit Price')
    
    def _query(self, with_clause='', fields={}, groupby='', from_clause=''):
fields['price_unit'] = ", s.price_unit as price_unit"
groupby += ', s.price_unit'
return super(PurchaseReport, self)._query(with_clause, fields, groupby, from_clause)



Avatar
Discard
Author

<?xml version="1.0" encoding="utf-8"?>
<odoo>

<record model="ir.ui.view" id="view_purchase_order_inherit_pivot">
<field name="name">product.month.pivot.inherit</field>
<field name="model">purchase.report</field>
<field name="inherit_id" ref="purchase.view_purchase_order_pivot"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='untaxed_total']" position="before">
<field name="price_unit" type="col"/>
<field name="price_unit" type="measure"/>
</xpath>
</field>
</record>
<record model="ir.ui.view" id="view_purchase_order_inherit_graph">
<field name="name">product.month.graph.inherit</field>
<field name="model">purchase.report</field>
<field name="inherit_id" ref="purchase.view_purchase_order_graph"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='untaxed_total']" position="before">
<field name="price_unit" type="row"/>
<field name="price_unit" type="measure"/>
</xpath>
</field>
</record>

</odoo>

Best Answer
Hi,

You try below  solution,  it  will  work  for  you.

class PurchaseReport(models.Model):
_inherit = "purchase.report"
 
picking_type_id = fields.Many2one('stock.warehouse', 'Warehouse', readonly=True)


def _select(self):
return super(PurchaseReport, self)._select() + ", spt.warehouse_id as picking_type_id"

def _from(self):
return super(PurchaseReport, self)._from() + " left join stock_picking_type spt on (spt.id=po.picking_type_id)"

def _group_by(self):
return super(PurchaseReport, self)._group_by() + ", spt.warehouse_id"


Thanks and Regards,
Sunny Sheth
Avatar
Discard

I am using this but I have an error when click the field in pivot view as LINE 3: ...ehouse" as "purchase_report__picking_type_id" ON ("purchase_...

Best Answer

You can just add new fields defination then add that fields is in Select and used in Groupby here we used l.price_unit because l meanse purchase_order_line.

Just apply this code and checked may be fixed your issue.

class PurchaseReport(models.Model):
       _inherit = "purchase.report"

       price_unit = fields.Many2one('purchase.order.line', string='Unit Price')

       def _select(self):
               return super(PurchaseReport, self)._select() + ", l.price_unit as price_unit"

       def _group_by(self):
               return super(PurchaseReport, self)._group_by() + ", l.price_unit"

Avatar
Discard
Author Best Answer

@Sunny and @Farid Thank you for taking your time to help.


Avatar
Discard