Skip to content

Commit b7ada82

Browse files
committed
Merge pull request OCA#118 from lepistone/fill-in-required-pricelist
fill in a required field without warnings
2 parents 2c0cd63 + 5878cae commit b7ada82

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

purchase_requisition_multicurrency/model/purchase_requisition.py

+33
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#
2020
#
2121
from openerp import models, fields
22+
from openerp.tools import SUPERUSER_ID
2223

2324

2425
class PurchaseRequisition(models.Model):
@@ -38,3 +39,35 @@ class PurchaseRequisition(models.Model):
3839
related='pricelist_id.currency_id',
3940
comodel_name='res.currency',
4041
string='Currency')
42+
43+
def _auto_init(self, cr, context):
44+
"""Fill in the required field with default values.
45+
46+
This is similar to the solution used in mail_alias.py in the core.
47+
48+
The installation of the module will succeed with no errors, and the
49+
column will be required immediately (the previous solution made it
50+
required only on the first module update after installation).
51+
52+
"""
53+
54+
# create the column non required
55+
self._columns['pricelist_id'].required = False
56+
super(PurchaseRequisition, self)._auto_init(cr, context=context)
57+
58+
default_pricelist_id = self.pool['product.pricelist'].search(
59+
cr, SUPERUSER_ID, [('type', '=', 'purchase')], limit=1,
60+
context=context
61+
)[0]
62+
63+
# do not use the ORM, because it would try to recompute fields that are
64+
# not fully initialized
65+
cr.execute('''
66+
UPDATE purchase_requisition
67+
SET pricelist_id = %s
68+
WHERE pricelist_id IS NULL;
69+
''', (default_pricelist_id,))
70+
71+
# make the column required again
72+
self._columns['pricelist_id'].required = True
73+
super(PurchaseRequisition, self)._auto_init(cr, context=context)

purchase_requisition_transport_document/tests/test_generate_po.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def setUp(self):
77

88
self.Requisition = self.env['purchase.requisition']
99

10-
self.requisition = self.Requisition.create({})
10+
self.requisition = self.Requisition.new({})
1111
self.supplier = self.browse_ref('base.res_partner_1')
1212

1313
self.doc1 = self.browse_ref('purchase_transport_document.CMR')

0 commit comments

Comments
 (0)