Odoo Help

Welcome!

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.

0

Odoo 9: Set domain for field in One2many

By
Matthew Watkins
on 7/28/16, 7:29 PM 449 views

I have three models:

class Team(models.Model):
    _name = 'myModule.team'
    _description = 'Team'
    name = fields.Char('Name')
   employee_link_ids = fields.One2many('myModule.link', 'team_id', 'Team Members')

class Employee(models.Model):
    _name = 'new_module.employee'
    _description = 'Employee'
    name = fields.Char('Name')

class Link(models.Model):
    _name = 'myModule.link'
    _description = 'Links Employee to Team and allows the user to assign a role to the employee.'
    role = fields.Char('Role')
    employee_id = fields.Many2one('myModule.employee', 'Employee')
    team_id = fields.Many2one('myModule.team', 'employee_line_ids')

With the corresponding view:

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

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

<field name="model">myModule.team</field>

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

<form>

<sheet>

<label for="name" class="oe_edit_only"/>

<h1>

<field name="name"/>

</h1>

<group>

<field name="employee_link_ids">

<tree editable="bottom">

<field name="employee_id"/>

<field name="role"/>

</tree>

</field>

</group>

</sheet>

</form>

</field>

</record>

 

When I add a Link record to the One2many with a particular Employee, I don't want that employee to show up in the employee_id drop downs for any existing or future child Link records for this Team.

The Employee should still be available for all other Teams where they haven't been used.

The feature should be workable before the record is saved.

I've tried a lot things including passing a list of values containing the IDs of the employee_id fields for each Link in the One2many to all the child Links, and also trying to put the list of values into context to be. Nothing has worked so far.

Does anyone have any ideas how this could be approached?

Thanks!

0
Vo Tri
On 8/4/16, 5:39 AM

You try: fields_view_get . You can search in your odoo:

Example:

@api.model
def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False):
res = super(AccountInvoiceLine, self).fields_view_get( view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu)
if self._context.get('type'):
doc = etree.XML(res['arch'])
for node in doc.xpath("//field[@name='product_id']"):
if self._context['type'] in ('in_invoice', 'in_refund'):
 node.set('domain', "[('purchase_ok', '=', True)]")
else:
node.set('domain', "[('sale_ok', '=', True)]")
res['arch'] = etree.tostring(doc)
return res

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)

Stats

Asked: 7/28/16, 7:29 PM
Seen: 449 times
Last updated: 8/4/16, 5:39 AM