Help

0

Odoo10: Dynamic domain

Hi, I am trying to add dynamic domain in Odoo 10 Many2one field. Here is the code:


    @api.multi

    def _get_education_domain(self):

        res = {}

        education_list = []

        if self.emeu_sector_id.id:

            education_ids = self.emeu_sector_id.emeu_education_ids

            education_list = [x.id for x in education_ids]

        return [('id', 'in', education_list)]


emeu_education_id = fields.Many2one(

        'emeu.education', string=_('Education'), ondelete='restrict', domain=_get_education_domain)


This throws JSON serialiser error. Can anyone please help me to fix the issue.

 

Avatar
Discard
2 Answers
3
Avatar
IT Libertas
Best Answer

Hey,

try to keep a value of domain in a separate field. E.g.:

@api.multi
@api.depends("emeu_sector_id.emeu_education_ids")
def _get_education_domain(self):
        for object in self:
              education_list = []
              if object.emeu_sector_id.id:
                   education_ids = object.emeu_sector_id.emeu_education_ids
                   education_list = [x.id for x in education_ids]               object.education_list = [(6,0, education_list)]
education_list = fields.Many2many('emeu.education',store=True,compute=_get_education_domain) emeu_education_id = fields.Many2one('emeu.education', string=_('Education'), ondelete='restrict'])

And on the xml form:

<field name="education_list" invisible="1"/> 
<field name="emeu_education_id" domain="[('id','in',education_list[0][2])]" />
7 Comments
Avatar
Discard
Avatar
dirtyHandsPHP
-

Hey, thanks for your answer but now it's throwing an error:

TypeError: 'Many2many' object does not support indexing

Avatar
IT Libertas
-

Does the domain field is filled properly?

If yes, try one of the followings: to remove [0][2] OR instead put ids

Avatar
dirtyHandsPHP
-

Domain field is populated properly. I already tried both of them and throwing following error:

RuntimeError: maximum recursion depth exceeded while calling a Python object

Avatar
dirtyHandsPHP
-

Hey, I got it fixed. Remove domain attribute from class field emeu_education_id and added in view:

<field name="emeu_education_id" domain="[('id','in',education_list[0][2])]" />

Avatar
dirtyHandsPHP
-

Please update your answer with above comment so that I can accept your answer. Much thanks for your help!! :)

Avatar
IT Libertas
-

glad to hear! the answer was updated

Avatar
dirtyHandsPHP
-

Thanks man!! :)

0
Avatar
Tomás Pascual
Best Answer

Vualá, amazing¡¡¡

I have remove [0][2] for domain xml view, I don't know exactly what this is for.

Thanks man¡¡¡¡

Avatar
Discard