Search code examples
odoo-8openerp-8

Odoo 8 display stock_move.picking_id.partner_id in view


I'm new to Odoo and am still trying to get the hang of its views.

I need to add a field "Supplier" in the standard view for Purchases - Incoming products, the value for which should be taken from the stock_move.picking_id.partner_id I can't seem to figure out how to get that value in the view xml, although the necessary relations seem to be defined, as I can get it this way in the python code.

Here's the standard view definition:

        <record id="view_move_tree_receipt_picking" model="ir.ui.view">
        <field name="name">stock.move.tree2</field>
        <field name="model">stock.move</field>
        <field name="priority" eval="6"/>
        <field name="arch" type="xml">
            <tree colors="grey:state == 'cancel'" string="Moves">
                <field name="date" widget="date"/>
                <field name="picking_id" string="Reference" invisible="1"/>
                <field name="origin"/>
                <field name="product_id"/>
                <field name="product_uom_qty"/>
                <field name="product_uom" string="Unit of Measure" groups="product.group_uom"/>
                <field name="location_id" invisible="1"/>
                <field name="location_dest_id" invisible="1"/>
                <field name="create_date" invisible="1"/>
                <field name="date_expected" invisible="1"/>
                <button name="%(stock.move_scrap)d"
                    string="Scrap Products" type="action"
                    icon="terp-gtk-jump-to-ltr" context="{'scrap': True}"
                    states="draft,waiting,confirmed,assigned"
                    groups="stock.group_stock_user"/>
                <field name="state"/>
                <button name="action_done" states="draft,assigned,confirmed"
                    icon="gtk-go-forward" type="object" groups="stock.group_stock_user"
                    class="oe_highlight" help="Done"/>
            </tree>
        </field>
    </record>

And the relevant column definitions (skipping unrelated lines for brevity)

class stock_move(osv.osv):
    _name = "stock.move"
    _description = "Stock Move"
    _order = 'date_expected desc, id'

    _columns = {
        'picking_id': fields.many2one('stock.picking', 'Reference', select=True, states={'done': [('readonly', True)]}),
    }


class stock_picking(osv.osv):
    _name = "stock.picking"
    _inherit = ['mail.thread']
    _description = "Picking List"
    _order = "priority desc, date asc, id desc"

    _columns = {
        'partner_id': fields.many2one('res.partner', 'Partner', states={'done': [('readonly', True)], 'cancel': [('readonly', True)]}),
    }

Solution

  • Do not need to define relation it's already there, so you just need to add related field in stock.move and add that partner field in stock.move's list view or form view by inheriting existing views.

    class stock_move(osv.osv):
        _inherit = "stock.move"
    
        _columns = {
            'partner_id': fields.related('picking_id', 'partner_id', 'Supplier', type='many2one', store=True, readonly=True),
        }
    

    Now add this partner_id field in existing view using inheritance.

    Base View ID => stock.view_move_form (it might differ in your case)

    <record id="new_view_id" model="ir.ui.view">
        <field name="name">stock.form</field>
        <field name="model">stock.move</field>
        <field name="inherit_id" ref="stock.view_move_form" />
        <field name="priority" eval="40"/>
        <field name="arch" type="xml">
            <!-- field name which you specify here after then new field will be added. -->
            <field name="existing_field_name" position="after">
                <field name="partner_id" />
            </field>
        </field>
    </record>