19
19
#
20
20
#
21
21
from openerp import models , fields
22
+ from openerp .tools import SUPERUSER_ID
22
23
23
24
24
25
class PurchaseRequisition (models .Model ):
@@ -38,3 +39,35 @@ class PurchaseRequisition(models.Model):
38
39
related = 'pricelist_id.currency_id' ,
39
40
comodel_name = 'res.currency' ,
40
41
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 )
0 commit comments