Search code examples
pythoninheritanceodooqweb

Inherit selection field from stock into another model - Odoo v9 community


I'm inheriting two models into my module, like this:

class fleet_vehicles_services(models.Model):
    _inherit = ["fleet.vehicle.log.services","stock.picking",]

Now, I have these two fields, which are related to stock.picking:

stock_picking = fields.Many2one("stock.picking", "Picking")
state = fields.Selection(string="Estados", store=True, readonly=True, related="stock_picking.state")

These are the states of the picking, now on my view:

    <record model='ir.ui.view' id='fleet_vehicle_log_services_form_inherit_1'>
        <field name='name'>fleet.vehicle.log.services.form0</field>
        <field name='model'>fleet.vehicle.log.services</field>
        <field name='inherit_id' ref='fleet.fleet_vehicle_log_services_form'/>
        <field name="priority">89</field>
        <field name='arch' type='xml'>
            <xpath expr="//form//sheet//group[1]" position="before">
            <header>
                <field name="state" widget="statusbar" statusbar_visible="draft,confirmed,assigned,done" statusbar_colors='{"waiting":"blue","confirmed":"blue"}'/>
            </header>
            </xpath>
        </field>
    </record>

But it throws me:

ParseError: "Invalid view definition

Error details:
Field `state` does not exist

Error context:
View `fleet.vehicle.log.services.form0`
[view_id: 2084, xml_id: n/a, model: fleet.vehicle.log.services, parent_id: 820]
None" while parsing /home/kristian/odoov9/danisan/fleet_stock/views/fleet_vehicle_log_services.xml:4, near
<record model="ir.ui.view" id="fleet_vehicle_log_services_form_inherit_1">
        <field name="name">fleet.vehicle.log.services.form0</field>
        <field name="model">fleet.vehicle.log.services</field>
        <field name="inherit_id" ref="fleet.fleet_vehicle_log_services_form"/>
        <field name="priority">89</field>
        <field name="arch" type="xml">
            <xpath expr="//form//sheet//group[1]" position="before">
            <header>
                <field name="state" widget="statusbar" statusbar_visible="draft,confirmed,assigned,done" statusbar_colors="{&quot;waiting&quot;:&quot;blue&quot;,&quot;confirmed&quot;:&quot;blue&quot;}"/>
            </header>
            </xpath>
        </field>
    </record>

I don't get it, I mean, I know this is an inherited model, but it should catch them, otherwise, how can I show this on that view?

Any ideas?


Solution

  • Python Code:

    class fleet_vehicles_services(models.Model):
        _inherit = "fleet.vehicle.log.services"
    
        stock_picking = fields.Many2one("stock.picking", "Picking")
        state = fields.Selection(string="Estados", store=True, readonly=True, related="stock_picking.state")
    

    XML Code:

    <record model='ir.ui.view' id='fleet_vehicle_log_services_form_inherit_1'>
            <field name='name'>fleet.vehicle.log.services.form0</field>
            <field name='model'>fleet.vehicle.log.services</field>
            <field name='inherit_id' ref='fleet.fleet_vehicle_log_services_form'/>
            <field name="priority">89</field>
            <field name='arch' type='xml'>
                <xpath expr="//form//sheet//group[1]" position="before">
                <header>
                    <field name="state" widget="statusbar" statusbar_visible="draft,confirmed,assigned,done" statusbar_colors='{"waiting":"blue","confirmed":"blue"}'/>
                </header>
                </xpath>
            </field>
        </record>