This question has been flagged

I know I can filter Many2one fields, from python code, or even xml views, with the domain flag, but I have a slightly different scenario right now,

I have these models:

class MyModel(models.Model):
    _name = 'mymodel'

    fieldsel = fields.Selection([('sheet', 'Sheet'),('reel','Reel')], string='Printing PPT Type', 
    track_visibility='onchange', copy=False,
    help=" ")
    fieldmany = fields.Many2one('text.paper', string="Text Paper")

The text.paper model has another Selection field, which has the same values as fieldsel, however, I cannot use domain since it will filter every text.paper statically.

My issue is, that I need to filter text.paper depending on which option I choose from fieldsel, so, let's say text.paper looks something like this:

class text_paper(models.Model):
    _name = 'text.paper'

    name = fields.Char(string="Code")
    paper_type = fields.Selection([('sheet', 'Sheet'),('reel','Reel')], string="Paper Type")

I need to filter from mymodel the text.paper depending on the fieldsel field, if reel selected, filter text.paper which are reel, and if sheet selected, filter text.paper accordingly.

So far, I've tried this:

@api.onchange('printing_text_type', 'printing_ppt_type', 'printing_add_text_type', 'printing_cover_type', 'printing_end_paper_type')
def change_domain(self):
    self.text_paper = False 
    if self.printing_text_type or self.printing_ppt_type or self.printing_add_text_type or self.printing_cover_type or self.printing_end_paper_type : 
        return {'domain': {text_paper: [('paper_type', '=', self.printing_text_type)], text_paper: [('paper_type', '=', self.printing_ppt_type)], text_paper: [('paper_type', '=', self.printing_add_text_type)], text_paper: [('paper_type', '=', self.printing_cover_type)], text_paper: [('paper_type', '=', self.printing_end_paper_type)]}}
    else:
        return {'domain': {text_paper: []}}

But it throws me this:

Traceback (most recent call last):
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\.\openerp\http.py", line 546, in _handle_exception
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\.\openerp\http.py", line 597, in dispatch
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\.\openerp\http.py", line 535, in _json_response
File "simplejson\__init__.pyc", line 354, in dumps
File "simplejson\encoder.pyc", line 264, in encode
File "simplejson\encoder.pyc", line 612, in _iterencode
File "simplejson\encoder.pyc", line 568, in _iterencode_dict
File "simplejson\encoder.pyc", line 568, in _iterencode_dict
File "simplejson\encoder.pyc", line 523, in _iterencode_dict
File "simplejson\encoder.pyc", line 484, in _stringify_key
TypeError: key <class 'openerp.addons.bsi.models.models.text_paper'> is not a string

Any ideas?



Avatar
Discard