2017-06-02 19 views
0

我使用的是Odoo 8 API v8,我想在域中動態添加域。 我有2個班。一位媽媽級的採購如何在Odoo 8中動態添加域

TMP_BESOIN = {} 
TMP_BESOIN['type']= '' 


_name = 'purchase.besoin' 
_description = "Expression du besion d'achat" 

name = fields.Char(required=True,string="Reference",default='XXX',readonly=True) 
employee_id = fields.Many2one('hr.employee', string="Demandeur", required=True) 

date_expression_besoin = fields.Datetime(string="Date d'expression", required=True,default=fields.Datetime.now()) 

demandeprix_id = fields.Many2one('purchase.demandeprix.ligne', string='Demande de prix') 

lignebesoin_ids = fields.One2many('purchase.besoin.ligne','besoin_id', string='Le besoin') 

type = fields.Selection([('product', 'Produit stockable'), ('consu', 'Utilisation directe'), ('immo', 'Immobilisation')], 
'Type d\'articles',defaut='product') 

order_ids = fields.One2many('purchase.order','besoin_id', string='Le besoin') 

destination = fields.Char(string='destination') 

demandeprix_ids = fields.One2many('purchase.demandeprix','besoin_id', string='ligne demande de prix') 

date_reject = fields.Datetime(string="Date de rejet", readonly=True) 

user_reject = fields.Many2one('hr.employee', 'Rejété par', readonly=True) 

date_validate = fields.Datetime(string="Date de Validation", readonly=True) 

user_validate = fields.Many2one('hr.employee', 'validé par', readonly=True) 

date_authorized = fields.Datetime(string="Date de d'autorisation", readonly=True) 

user_authorized = fields.Many2one('hr.employee', 'autorisé par', readonly=True) 

date_wait_authorisation = fields.Datetime(string="Date de transmition pour autorisation", readonly=True) 

user_wait_authorisation = fields.Many2one('hr.employee', 'Transmit par', readonly=True) 

date_done = fields.Datetime(string="Date de clôture", readonly=True) 

user_done = fields.Many2one('hr.employee', 'clôturé par', readonly=True) 

motivation = fields.Text('Designation') 
state = fields.Selection([('draft', 'Brouillon'), 
           ('reject', 'Besoin annulé'), 
           ('validate', 'Besoin validé'), 
           ('stock_verified', 'Stock vérifié'), 
           ('wait_authorized', 'En attente d\'autorisation'), 
           ('authorized', 'Autorisé') 
           ], 'Etat',required=True,default='draft') 

_sql_constraints = [ 
('ref_besoin_achat_uniq', 'unique(name)', 'La reference du besoin est unique'), 
] 

@api.model 
def create(self,vals): 
    if not vals.has_key('name') or vals.get('name')=='XXX': 
     vals['name'] = self.env['ir.sequence'].get('purchase.besoin.achat.code') or 'XXX' 
    return super(purchase_besoin,self).create(vals) 

@api.onchange('type') 
def on_change_type(self): 
    global TMP_BESOIN 
    TMP_BESOIN = {} 
    TMP_BESOIN['type'] = self.type 

這個類中定義的全局變量TMP_BESOIN,並把價值方法onchange_tye

這個類是有關many2one與另一CLASSE ligne_besoin

類purchase_besoin_ligne(models.Model):

_name = 'purchase.besoin.ligne' 
_description = "Ligne du besoin d'achat" 

name = fields.Char('Nom', readonly=True, store=True) 

besoin_id = fields.Many2one('purchase.besoin', string='Besoin', required=True, ondelete='cascade') 

besoin = TMP_BESOIN.copy() 

produit_id = fields.Many2one('product.product', string="Article", required=True,domain=[('product_tmpl_id.type', 'like', besoin['type'])]) 

qte = fields.Integer(string="Quantité", required=True,default=1) 

type = fields.Selection([('product', 'Produit stockable'), ('consu', 'Utilisation directe'), ('immo', 'Immobilisation')], 
'Type d\'articles',defaut=besoin['type']) 

和我的代碼一樣,我想在produit_id中添加一個域,其中域的值取決於th母類中的變量類型。

正如我不能有變量besoin_id(同母類的鏈接)的調用create方法之前的值。我決定用一個全局變量

的問題是,該過濾器不會沒有錯誤適用。

回答

0

嘗試以下方法:假設類型是product.product在produit_id的域的列。

_name = 'purchase.besoin.ligne' 
_description = "Ligne du besoin d'achat" 

name = fields.Char('Nom', readonly=True, store=True) 

besoin_id = fields.Many2one('purchase.besoin', string='Besoin', required=True, ondelete='cascade') 

besoin = TMP_BESOIN.copy() 

produit_id = fields.Many2one('product.product', string="Article", required=True,domain=[('type', 'like', besoin_id.type])]) 

qte = fields.Integer(string="Quantité", required=True,default=1) 

type = fields.Selection([('product', 'Produit stockable'), ('consu', 'Utilisation directe'), ('immo', 'Immobilisation')],'Type d\'articles',defaut=besoin['type']) 
+0

類型不是product.product但product.template其與product.product –

+0

鏈接many2one OK,然後把product_tmpl_id.type域中,讓我知道 –

+0

早上會發生什麼的列。我但它不起作用。選擇內容爲空 –