This question has been flagged
4259 Views

I'm trying to restrict a Many2one (foreign key) field selection in fairly basic way. (This is the whole "project" https://github.com/tarpas/odoo_playground/tree/domain_against_computed_field )

Let's say I have this model:

class Course(models.Model):    
_name = 'openacademy.course'
name = fields.Char(string="Title", required=True)
parent_course_id = fields.Many2one('openacademy.course', ondelete='set null')


I know it's possible to specify <field name="parent_course_id" domain="[('name', '=', course]" /> in the form.

However I only want the user pick the records where the 2nd character of name is 'A', so I need python expressions to filter out the records. Let's assume it cannot be a front-end domain.

How do I go about this? I tried multiple approaches but to no avail. On of them was to establish a new computed One2many field in Course class:

allowed_parent_ids = fields.One2many('openacademy.course', 'parent_course_id', compute="compute_allowed_parents" )


 and the method in Course class

@api.depends('parent_course_id')
def compute_allowed_parents(self):
    result = []
    for course in self.env['openacademy.course'].search([]):
        if course.name[1:2] == 'A':
            result.append(course.id)
    self.allowed_parent_ids = result


Then in the form for Course in openacademy.xml I try to use the computed field in domain

   <record model="ir.ui.view" id="course_form_view">

<field name="name">course.form</field>

<field name="model">openacademy.course</field>

<field name="arch" type="xml">

<form string="Course Form">

<sheet>

<group>

<field name="name"/>

<field name="parent_course_id" domain="[('parent_course_id', 'in', allowed_parent_ids)]" />

<field name="allowed_parent_ids" />

........etc

And I get the exception:

 File "/Users/tibor/Documents/odoows/openerp/openerp/api.py", line 250, in wrapper

    return old_api(self, *args, **kwargs)

  File "/Users/tibor/Documents/odoows/openerp/openerp/api.py", line 354, in old_api

    result = method(recs, *args, **kwargs)

  File "/Users/tibor/Documents/odoows/openerp/openerp/models.py", line 1738, in name_search

    return self._name_search(name, args, operator, limit=limit)

  File "/Users/tibor/Documents/odoows/openerp/openerp/api.py", line 248, in wrapper

    return new_api(self, *args, **kwargs)

  File "/Users/tibor/Documents/odoows/openerp/openerp/api.py", line 490, in new_api

    result = method(self._model, cr, uid, *args, **old_kwargs)

  File "/Users/tibor/Documents/odoows/openerp/openerp/models.py", line 1750, in _name_search

    ids = self._search(cr, user, args, limit=limit, context=context, access_rights_uid=access_rights_uid)

  File "/Users/tibor/Documents/odoows/openerp/openerp/api.py", line 250, in wrapper

    return old_api(self, *args, **kwargs)

  File "/Users/tibor/Documents/odoows/openerp/openerp/models.py", line 4762, in _search

    cr.execute(query_str, where_clause_params)

  File "/Users/tibor/Documents/odoows/openerp/openerp/sql_db.py", line 141, in wrapper

    return f(self, *args, **kwargs)

  File "/Users/tibor/Documents/odoows/openerp/openerp/sql_db.py", line 220, in execute

    res = self._obj.execute(query, params)

TypeError: not all arguments converted during string formatting


Avatar
Discard

your query is still unclear , please explain where you are using domain="[("parent_course_id", 'in', allowed_parents)]" and where you have define allowed_parents fields

Author

I tried to clarify. allowed_parents and compute allowed parents are in the Course class. the domain is a in an xml..

Author

I pushed the "project" to github here https://github.com/tarpas/odoo_playground/tree/domain_against_computed_field It's a standalone minimal module, which demonstrates the problem.