0
votes

I work with Odoo 8 API v8 and I want to dynamically add a domain in a field. I have 2 classes. A mother class purchase

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

This class defined global variable TMP_BESOIN and put value in method onchange_tye

this class is in relation many2one with another classe ligne_besoin

class 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'])

Like as in my code, I want to add a domain in produit_id, in which the value of the domain, depends on the variable type in the Mother class.

As I cannot have the value of the variable besoin_id(link with the mother classes) before calling create method. I decided to use a global Variable

The problem is that, the filter doesn't apply without an error.

1

1 Answers

0
votes

try the following: assuming that type is a column of product.product in the domain of 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'])