跳至内容
菜单
此问题已终结
2 回复
10877 查看

Hi, i am using odoo11.

I'd like to make a module that, when installed, simply adds some records to the database. For example, it should add some lines to the res_partner table (the content of those lines would simply be hardcoded in the module itself).

How can I accomplish that? My main problem is that i don't exactly know where to write my code so that it is executed during the installation of the module...

Thanks for any help 

形象
丢弃
最佳答案

Hi Daniele,

You can do this by creating an XML file that contains the data. When you add this XML file to the __manifest__.py file it will be loaded when installing (or updating) the Odoo module. An example of a data record:

<?xml version="1.0" ?>
<odoo>
    <record id="some_record" model="your.model">
        <field name="name">Name of the record</field>
    </record>
</odoo>

You can just load the XML file in the __manifest__ in order to have the data available when installing the app:

# always loaded
    'data': [
        'data/your_data_file.xml',
    ]

You can also find examples of this in the official Odoo code, for example in the app "projects".
Example of data: https://github.com/odoo/odoo/blob/11.0/addons/project/data/project_data.xml  
Example of loading the data: https://github.com/odoo/odoo/blob/e0b1718e4a1ab641f56fef242ae1d6c13254b53c/addons/project/__manifest__.py#L35

Regards,
Yenthe

形象
丢弃
编写者

Thank you very much for your kind answer!

You're welcome, best of luck!

最佳答案

Hello Daniele,

By using XML we can do it, see the following example sale order import with two lines while install the module.

<record id="sale_order_1" model="sale.order">

            <field name="partner_id" ref="base.res_partner_2"/>

            <field name="partner_invoice_id" ref="base.res_partner_2"/>

            <field name="partner_shipping_id" ref="base.res_partner_2"/>

            <field name="user_id" ref="base.user_demo"/>

            <field name="pricelist_id" ref="product.list0"/>

            <field name="team_id" ref="sales_team.team_sales_department"/>

            <field name="date_order" eval="(DateTime.today() - relativedelta(months=1)).strftime('%Y-%m-%d %H:%M')"/>

        </record>


        <record id="sale_order_line_1" model="sale.order.line">

            <field name="order_id" ref="sale_order_1"/>

            <field name="name">Laptop E5023</field>

            <field name="product_id" ref="product.product_product_25"/>

            <field name="product_uom_qty">3</field>

            <field name="product_uom" ref="product.product_uom_unit"/>

            <field name="price_unit">2950.00</field>

        </record>


        <record id="sale_order_line_2" model="sale.order.line">

            <field name="order_id" ref="sale_order_1"/>

            <field name="name">Pen drive, 16GB</field>

            <field name="product_id" ref="product.product_product_30"/>

            <field name="product_uom_qty">5</field>

            <field name="product_uom" ref="product.product_uom_unit"/>

            <field name="price_unit">145.00</field>

        </record>

形象
丢弃
编写者

Thanks!

相关帖文 回复 查看 活动
1
3月 15
7686
1
3月 15
3937
5
5月 25
2593
0
2月 25
645
2
11月 24
4622