Populate other field with value - odoo

I have a test many2one field. When it is populated I want the partner_id field to use the partner associated with that field. Following is not working:
<field name="partner_id" required="1"/>
<field name="x_test" context="{'partner_id': parent.partner_id}" />

you should try this :
<field name="x_test" context="{'default_partner_id': partner_id}" />
I don't know what you mean by parent.partner_id this works if you have a field named parent in the same view.

i assume you wanna put same value of partner_id in x_test field, then use related field
partner_id = fields.Many2one('res.partner', string="partner")
x_test = fields.Many2one('res.partner',related='partner_id', string="X Test")
in XML
<field name="partner_id" required="1"/>
<field name="x_test" />

Related

How to keep the order of the records added in a many2many field the way I wrote them?

I want to keep the order of the records in the many2many field, the way I added them using the write method, but Odoo use the alphanumerical order by default and overwrote the order I put them in. How can I keep it in the same order when I wrote them, is that possible ?
Here is the model code:
# -*- coding: utf-8 -*-
from odoo import models, fields, api
class projet_richat(models.Model):
_inherit = 'project.task'
competence_ids = fields.Many2many('competence.competence',string='Compétences')
consultants=fields.Many2many('hr.employee', string="Consultants")
def search_comp(self):
if len(self.competence_ids) != 0:
consults=self.env['hr.employee'].sudo().search([])
list=[]
for cmp in self.competence_ids:
print(cmp.competence)
for cons in consults:
print(cons.name)
for c in cons.competences:
if c.competence.competence==cmp.competence:
list.append((cons.id,c.competence.competence,c.niveau))
sorted_by_level = sorted(list, key=lambda tup: tup[2], reverse=True)
print(sorted_by_level)
for sorted_cons in sorted_by_level:
self.write({'consultants': [[4, sorted_cons[0]]]})
and here is the view code:
<odoo>
<data>
<record model="ir.ui.view" id="project_inherit_form">
<field name="name">project.inherit.form</field>
<field name="model">project.task</field>
<field name="type">form</field>
<field name="inherit_id" ref="project.view_task_form2"/>
<field name="arch" type="xml">
<field name="user_ids" position="before">
<field name="competence_ids" widget="many2many_tags"/>
</field>
<field name="parent_id" position="after">
<field name="consultants">
<tree>
<field name="name"/>
</tree>
</field>
</field>
<button name="action_assign_to_me" position="after">
<button name="search_comp" string="Test" class="btn-primary"
type="object"
/>
</button>
</field>
</record>
</data>
</odoo>
Thanks in advance.
The many2many field will use the list view to show its content, the default order is defined by the _order attribute on the related model.
You can alter the default ordering by setting the default_order parameter on the tree tag
From the List view documentation:
default_orderoverrides the ordering of the view, replacing the model’s order (_order model attribute). The value is a comma-separated list of fields, postfixed by desc to sort in reverse order:
<tree default_order="sequence,name desc">
To keep the order you want, you can add an integer field sequence and use it to order records (_order='sequence') then in search_comp function set the sequence field value for each line

Odoo Hide Many2one Field when Empty

I have a Many2one field that I want to hide when there is no value. I try from this solution, but it didn't work and give me an error when trying to upgrade.
<field name="parent_id" attrs="{'invisible': [('parent_id', '!=', False)]}"/>
I had this problem too and I solved this with define a dummy field in my model and use this in view like this:
'''
hide = fields.Boolean(string="Hide", compute="_set_hide", store=False)
#api.depends('parent_id')
def _set_hide(self):
if self.parent_id.id:
self.hide = False
else:
self.hide = True
'''
and in xml you should write like this:
<field name="hide" invisible="1"/>
<field name="parent_id" attrs="{'invisible': [('hide', '='True)]}"/>
If you want the field to be hidden when it is empty, the code is as follows:
<field name="parent_id" attrs="{'invisible': [('parent_id', '=', False)]}"/>

Odoo : limit editable rows on editable tree

Suppose I have an editable tree
<tree editable="top">
<field name="date">
<field name="value">
</tree>
Now suppose I want to let the user edit the values for 3 most recent dates, but the others should remain readonly.
How would I do that ?
Well, you could add a boolen field to the model. which will be a computed field. based on that field you could apply the read-only attrs as following:
class TheModel(models.Model):
_name = 'The.Model'
old_dated = fields.Boolean(compute='_old_dated_rec')
date = fields.Date()
value = fields.Integer()
#api.model
def _old_dated_rec(self):
"""define the condition of old dated records which could be as"""
recent_rec = self.search([], order='date desc', limit=3)
old_rec = self.search([('id', 'not in', recent_rec._ids)])
old_rec.write({'old_dated': True})
Then you could apply a scheduler to run everyday calling such method
<field name="old_dated" invisible="1" />
<field name="date" attrs="{'readonly':[('the_boolen_field','=',True)]}"/>
in such way, the compute method will update the boolean field.

Remove create and edit depending up on value of another field in odoo11

Need to remove create and edit in partner_id field in sales order depending on value of another field. I found one similar answer but it not working.
<field name="partner_id" position="replace">
<field name="partner_id" string="partner" domain=[('customer','=',True),('sale_invoice_type','=',sale_invoice_type)]" context="{'search_default_customer':1, 'show_address': 1,'default_sale_invoice_type':sale_invoice_type}" attrs=" {'invisible': [('sale_invoice_type', '=', 'cash')]}" options='{"always_reload": True, "no_create_edit": True}'/>
<field name="partner_id" domain="[('customer','=',True),('sale_invoice_type','=',sale_invoice_type)]" context="{'search_default_customer':1, 'show_address': 1,'default_sale_invoice_type':sale_invoice_type}" attrs="{'invisible': [('sale_invoice_type', '=', 'credit')]}" options='{"always_reload": True}'/>
</field>
When the sale_invoice_type is cash edit and create should be removed
First the domain of both field should look like this
<field .... attrs=" {'invisible': [('sale_invoice_type', '!=', 'cash')]}"/>
<field .... attrs=" {'invisible': [('sale_invoice_type', '=', 'cash')]}"/>
IF The solution didn't work because you should not have the same
field two time in your view Odoo will be confused witch one to pass.
But you can workaround it by creating a new field.
In your model define another partner field just a dummy one to use it instead of
the real partner_id but make sure that both field at the end of write and create
will always be equal.
partner_no_create = fields.Man.......
In your code make sure that this two field always equal:
# I think this onchage handle the cases in Odoo views
#api.onchange('partner_id')
def set_partner_no_create(self):
if self.sale_invoice_type != 'cash':
self.partner_no_create = self.partner_id
#api.onchange('partner_no_create')
def set_partner_no_create(self):
if self.sale_invoice_type == 'cash':
self.partner_id = self.partner_no_create
In your XML
<field name="partner_no_create" string="partner" domain="[('customer','=',True),('sale_invoice_type','=',sale_invoice_type)]"
context="{'search_default_customer':1, 'show_address': 1,'default_sale_invoice_type':sale_invoice_type}"
attrs=" {'invisible': [('sale_invoice_type', '!=', 'cash')]}" options='{"always_reload": True, "no_create_edit": True}'/>
<field name="partner_id" domain="[('customer','=',True),('sale_invoice_type','=',sale_invoice_type)]"
context="{'search_default_customer':1, 'show_address': 1,'default_sale_invoice_type':sale_invoice_type}"
attrs="{'invisible': [('sale_invoice_type', '=', 'cash')]}" options='{"always_reload": True}'/>
But still have to handle more cases specially when the record is updated or create from RPC call, you need to
override create and write method to handle all cases.

odoo - domain filter based on field from another class

So I have this 2 classes.
this
class Relocation(models.Model):
_name = 'test.plan_relocation'
type = fields.Selection()
price_id = fields.Many2one('test.plan_price',domain="[('type','=',type)]")
relocation_line_ids = fields.One2many('test.plan_relocation_line','relocation_id')
and this
class RelocationLine(models.Model):
_name = 'test.plan_relocation_line'
relocation_id = fields.Many2one('test.plan_relocation')
source_id = fields.Many2one('test.plan_spending_actual',required=True)
available_source = fields.Float(related='source_id.available',default=0,readonly=True)
The thing is I want to filter the "source_id" based on the "price_id" field.
How can I achieve that?
The 2 classes are in one xml. A part of the xml looks like this.
<field name="relocation_line_ids">
<tree editable="bottom">
<field name="source_id" string="Source" />
<field name="available_source" />
</tree>
</field>
Thank you so much for your help.
in xml of test.plan_relocation_line:
<field name="source_id" domain="[('price_id', '=',parent.price_id)]" />
Hope it will help you..