NeoVe
NeoVe

Reputation: 3907

Programmatically add default data to custom module - Odoo v9 community

I have a custom module, which inherits fleet.vehicle.log.services model

This is a custom module.

What I'm trying to do, is to add default data to just two fields of it, let me explain myself.

The model has these mandatory fields:

vehicle_id
cost_id
cost_type

And I've inherited/added these ones:

x_location_src_id
x_location_dest_id

These are stock locations, what I want is to add default stock locations to this model, but just the locations, not the mandatory fields.

So far, I've tried like this, in my data/location_data.xml file:

<record id="location_default" model="fleet.vehicle.log.services">
    <field name="vehicle_id">1</field>
    <field name="cost_id">2</field>
    <field name="cost_type">fuel</field>
    <field name="x_location_src_id">ReparacionUnidades</field>
    <field name="x_location_dest_id">ReparacionUnidades</field>
</record>

But it doesn't work for me, because, as I've said, I don't need the first three fields, just the last two ones.

So, as this is the only way to add default data by QWeb/XMl, I think there is a way to overcome this, by some attributes or maybe programmatically with python.

Do You have a clue on how to do it in some other way?

As I said, I want to bypass the fact that I have to add these other fields, because it makes no sense to me.

Any ideas?

Maybe some logic or attributes into stock.locations and not directly into this fleet.vehicle.log.services model?

Upvotes: 0

Views: 469

Answers (1)

Phillip Stack
Phillip Stack

Reputation: 3378

In the new api you can override the field from the inherited model and assign a default value either using a static value or a function. Here is an example

class Partner(models.Model):
    _inherit = 'res.partner'

ASSIGNING A STATIC DEFAULT VALUE

    email = fields.Char(default='[email protected]')

ASSIGNING A PROGRAMMATIC DEFAULT VALUE

    def _email_default(self):
        return '[email protected]'

    email = fields.Char(default=_email_default)

Using the old api you can do something similar, using a different syntax.

class Partner(osv.osv):
    _inherit = 'res.partner'

    _columns = {
        'email': fields.char('Email')  
    }

PROGRAMMATIC MEANS OF ASSIGNING DEFAULT VALUE

    def _email_default(self, cr, uid, ctx={}):
        return '[email protected]'

    _defaults = {
        'email': _email_default,
    }

STATIC METHOD OF ASSIGNING A DEFAULT VALUE

    _defaults = {
        'email': '[email protected]',
    }

Upvotes: 3

Related Questions