Odoo Help


This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Foreign key values offered based on a python expression.

Tibor Arpas
on 6/8/16, 9:54 AM 247 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

def compute_allowed_parents(self):
    result = []
    for course in self.env['openacademy.course'].search([]):
        if course.name[1:2] == 'A':
    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">



<field name="name"/>

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

<field name="allowed_parent_ids" />


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

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

Prakash Sharma
on 6/8/16, 12:32 PM

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

Tibor Arpas
on 6/8/16, 4:03 PM

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.

Tibor Arpas
on 6/9/16, 3:53 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)


Asked: 6/8/16, 9:54 AM
Seen: 247 times
Last updated: 6/9/16, 3:50 AM