In this tutorial I will learn you how to automatically fill a Many2many with data. In some cases a many2many always has to have data so in that case having it filled in automatically is really handy.
1. Creating the model
The first step is to create a new model with fields from which we want to get data. (in case you already have a model where you want to load from skip to chapter 2)
In my example I want to create a many2many which gets all records from the model sale.order.printorder and automatically adds them, so lets first create the model sale.order.printorder:
1
2
3
4
|
class many2many_default_data_demo(models.Model):
_name = ‘sale.order.printorder’
_description = ‘Model for many2many’
name = fields.Char(‘Name’)
|
2. Create and fill many2many
After the model is made we now have to fill up the many2many. Create a many2many to your other model and add the default parameter to it:
1
2
3
4
5
6
7
8
|
class print_order_sample(models.Model):
def _get_default_print_order_ids(self):
cr = self.pool.cursor()
self.env
return self.pool.get(‘sale.order.printorder’).search(cr, self.env.uid, [])
_inherit = ‘sale.order’
print_order_ids = fields.Many2many(‘sale.order.printorder’,‘sale_order_print’,‘print_id’,‘order_print_id’,‘Print order’,help=‘This could be used to create a print order for your report, for example.’,default=_get_default_print_order_ids)
|
As you can see there is a default=_get_default_print_order_ids which links to a function in the class. This function will automatically get all data from the model sale.order.printorder thanks to self.pool. After the pool got all records we will return all the data and it will be added to the many2many.
Now lets create a view that shows the many2many:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<record id=“view_orderinherit_orderlines” model=“ir.ui.view”>
<field name=“name”>sale.order.form.inherit.many2many</field>
<field name=“model”>sale.order</field>
<field name=“inherit_id” ref=“sale.view_order_form”/>
<field name=“arch” type=“xml”>
<!—Adds a new page (tab) to the view after the tab Information —>
<xpath expr=“//page[@string=’Order Lines’]” position=“after”>
<page name=“many2manydemo” string=“Many2many demo”>
<label for=“print_order_ids” string=“printorder records:”/>
<field name=“print_order_ids”/>
</page>
</xpath>
</field>
</record>
|
When a user would now create a new quotation/order there will be a new tab named ‘Many2many demo’ which is automatically filled with all records from the model sale.order.printorder:
3. Creating default records for sale.order.printorder
Want to go further and want to have the records generated by default when the module is installed? Then create a new XML file where you create new records. I’ve created a file defaultdata.xml in my module and added the following records:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
<?xml version=“1.0” encoding=“utf-8”?>
<openerp>
<data noupdate=“1”>
<!—This will create some default records if they do not exist yet. We will always need these so these are auto–rendered. —>
<record id=“goals_print_order” model=“sale.order.printorder”>
<field name=“name”>Goals</field>
</record>
<record id=“workmethod_print_order” model=“sale.order.printorder”>
<field name=“name”>Work method</field>
</record>
<record id=“approach_print_order” model=“sale.order.printorder”>
<field name=“name”>Approach</field>
</record>
<record id=“orderlines_print_order” model=“sale.order.printorder”>
<field name=“name”>Orderlines</field>
</record>
<record id=“about_print_order” model=“sale.order.printorder”>
<field name=“name”>About</field>
</record>
<record id=“customer_print_order” model=“sale.order.printorder”>
<field name=“name”>Customer</field>
</record>
<record id=“references_print_order” model=“sale.order.printorder”>
<field name=“name”>References</field>
</record>
</data>
</openerp>
|
TIP: Add the filename defaultdata.xml to __openerp__py under ‘data’.
When the module is installed these records will be automatically created in the database and will never be updated thanks to the noupdate=”1″. When you need a many2many filled with default data that always has to be the same this is perfect. If this is not the case simply create a new view where users can create new records in to the model sale.order.printorder!
Do you want to see the source code or try this module out? You can download / view it on Github!
Has this tutorial helped you, do you have any feedback or questions? Post away!