In OpenERP, How to show or hide a field based on Domain from its Parent (Many2One Object) - odoo

In OpenERP version 7, I need to show or hide a field in the Form View of the "Add Object" based on the values from the Parent Object.
For example, I have a field demo_field1 in sale_order. When I create a Sale Order Line, I do not want to show the field "th_weight" if the demo_field1 for the Sale Order is greater than 200.
using attrs="{'invisible': [('demo_field', '>', '200')]}" or attrs="{'invisible': [('order_id.demo_field', '>', '200')]}" shows invalid field in domain.
How to achieve this?

I also had the same issue some time before. What i did was to add a related field in the sale_order_line and define attriute based on that related field. ie;
In sale order line i defined a related field to the field demo_field1 as:
'test_field_new': fields.related('order_id', 'client_order_ref', string="Test Field", type="float")
But the related field will only be loaded at the time of saving the record. So i passed the default value of the field test_field_new from xml file as:
<field name="order_line" context="{'default_test_field_new': demo_field1}"/>
so that when i click on "Add new item" in one2many field, the value of the field demo_field1 will be loaded by default to test_field_new and i defined the attribute using the field test_field_new.
<field name="price_unit" attrs="{'invisible': [('test_field_new', '>', 200)]}"/>
I am not sure this is a clean method to do...

Related

conditions for a field to appear in odoo 14

I'd like field A to appear only if field B has a given value
field B doesn't belong to the same model, it belongs to a model connected through a many2one relationship
How do I write the contents of the attrs attribute for field A in the form ?
Until now I used
<field name="A"...
attrs="{'invisible': [('B', '=', 'some_value')
But until now both A and B belonged to the same model
Field used in attrs must be present in view
You can't use dotted field names because Odoo will raise an error if the field name contain . when validating the view definition:
Invalid composed field %(definition)s in %(use)s (attrs.invisible ...)
There is an exception for list sub-views (One2many/Many2many display in a form view) where you can use parent.field_name
ok, I reply to myself
First, in the model:
field_B = field.fieldtype(related='many2one_field.field_b')
Then, in the form view:
<field name="field_B" invisible="1"/>
and then
<field name="field_A" ...
attrs="{'invisible': [('field_B', '=', 'some_value')]}">

how to hide field in one2many in odoo

I have fields on one2many tree view (check box which is boolean field and salesperson which is many2one).
Now, I want to hide a field in one2many tree view based on a boolean field.
when checkbox(which is in one2many field) is true then salesperson(field_name=salesman_id) field is hide(which is also in one2many field).
I used below code but not work.
<xpath expr="//form[1]/sheet[1]/notebook[1]/page[1]/field[#name='order_line']/tree[1]/field[#name='price_unit']" position="after">
<field name="salesman_id" attrs="{'invisible':[('checkbox', '=', True)] }"/>
</xpath>
Please, Find the attachment. enter image description here
How to do that.
Thanks in advance.
The tree view field invisible attribute will not hide the column of the tree view completely, it will just hide the field value, because others rows could contain values that cause the column to show a value for that row.

How to allow only create option in many2one field

How can I hide all the items from the many2one field. I want to allow the user only to create a new payment term.
We have this code in account module in account_invoice.py:
class AccountInvoice(models.Model):
_name = "account.invoice"
payment_term_id = fields.Many2one('account.payment.term',
string='Payment Terms',
oldname='payment_term',
readonly=True,
states={'draft': [('readonly', False)]})
In account_invoice_view.xml we have:
<field name="payment_term_id"
options="{'no_create': True}"
attrs="{'invisible': [('payment_term_id','=',False)]}"/>
I tried this code {'no_open':True} but it didn't work.
If you want to hide all the elements on the list try adding a domain that is always False:
<field name="id" />
<field name="payment_term_id"
domain="[('id', '=', -1)]"
attrs="{'invisible': [('payment_term_id','=',False)]}"/>
With options="{'no_create': True}" you get the opposite of what you want if I understood well
I think you going to find this solution good for you:
what is suggest is add a field to your model this field is many2many field
contains the list of payment_term_id that are created in the current record.
# this field is for technical purpose
create_payment_terms_ids = fields.Many2many(co_model='account.payment.term',
relation='created_payment_rel',
column1= 'invoice_id',
column2= 'paymen_id',
string='Created payment terms')
After this use onchange method to keep track of created payments and add the new
Payment terms to this field
#api.onchange('payment_term_id')
def onchange_payment_terms(self):
""" keep track of created payment from the current
invoice and show only them in drop down list."""
if self.payment_term_id:
# keep list of old ids here
payment_ids = self.create_payment_terms_ids and self.create_payment_terms_ids.ids or []
# check if list payment is empty this means that this is the first created payment
# second check if the selected payment is a new payment that is created
# if one of the two is true add the selected value to the list of payment
if not self.create_payment_terms_ids or self.payment_term_id.id not in payment_ids:
payment_ids.append(self.payment_term_id.id)
self.create_payment_terms_ids = [(4, self.payment_term_id.id)]
# if list is not empty we change the domain to show only payment that exist in the list
# else we use a special domain to show empty set.
domain = payment_ids and [('payment_term_id', 'in', payment_ids)] or [('id', '=', -1)]
return {'domain': {'payment_term_id': domain}}
In your view add the new field with visible = "1" you don't the user to see it's value.
you need to put it in the view because you need it's value in onchange event if you don't put it your many2many field will always be empty.
<field name="create_payment_terms_ids" invisible="1"/>
and remove options="{'no_create':False}" this will remove the create and edit option in the drop down and you don't want that.
Note: when you are in development remove invisible="1" to see if the many2many field contains the list of payment that you have
created.

How to add null value to field Many2one from data file Odoo

I want to add a new category of Salary Structure by data file. Here is my current code
<record id="MEALS" model="hr.salary.rule.category">
<field name="name">Meals</field>
<field name="code">MEALS</field>
<field name="parent_id" eval=""></field>
</record>
All I need is a category with no parent category but with these code parent category of it is Base for new structures. I've tried with evals="0", evals = " " or remove field parent_id out of .How can I add null value into this field ?
This behaviour is a little weird .AFAIK If you don't include the parent_id field, it should work and have a null value because that field doesn't have a default value on it. (Except you've extended the model and added a default on that field).
The only fields where null values don't work in the Odoo ORM are Integer and float fields. (That's the reason they always have an initial value of 0 or 0.0 in a form view).
quoting the docs:
field
Each record can be composed of field tags, defining values to
set when creating the record. A record with no field will use all
default values (creation) or do nothing (update).
A field has a mandatory name attribute, the name of the field to set,
and various methods to define the value itself:
Nothing
if no value is provided for the field, an implicit False will
be set on the field. Can be used to clear a field, or avoid using a
default value for the field.
Try this <field name="parent_id" /> or <field name="parent_id" eval="False" />
If the above don't work then double check your codebase and make sure you're not setting any default value on the field.

Odoo show values of selected Many2one record

In my custom view, there is a field Many2one, next to it I would like to show values of that item as information in view after a serial_number is selected.
# model.py (newApi)
serial_number = fields.Many2one(comodel_name="stock.production.lot", string="Serial Number", required=True)
# view.xml
<field name="serial_number" options="{'no_open': True, 'no_create': 1, 'no_create_edit': 1}"/>
<div>
<span>serial_number.product_id.name</span>
<span>serial_number.product_id.description</span>
</div>
How I have to do correctly?
As per your requirement you need to use widget='selection' This will make many2one field as a selection field.
try with this:
<field name="serial_number" widget='selection'/>
USE related to do this :
Create a related fild in the model that have many2one relation
like this
field_name = fields.Char(string="Selected value", related="Field_name_in_co_model")
than put it in your form with readOnly :
<field name="field_name" readonly="1" />
when the user select an item the information of the selected record will show automaticly in the fields sorry for my english if you didn't undertand tell me i will give an exemple
you can even put it in tree form it's greate thing