How to add null value to field Many2one from data file Odoo - 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.

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.

Is it possible to change relation of a field based on a condition?

Suppose there is a field 'A' and has an fields declaration say
A:fields.many2one('new.new')
and then based on any condition, the same field should get a different relation like
if condition:
A:fields.many2one(old.old)
Is it possible?
AFAIK the answer is No. You cannot change at runtime the comodel of a m2o field. It would require to change the Foreign Key and other IR relations which are created when the module is installed/updated.
Maybe you can achieve something similar by creating both
A: fields.many2one('old.old')
B: fields.many2one('new.new')
C: fields.Boolean()#condition, eventually computed
and then using A or B depending on your condition.
for the view part:
<field name="C" invisible="1"/>
<field name="A" attrs="{'invisible': [('C','=',False)]}"/>
<field name="B" attrs="{'invisible': [('C','=',True)]}"/>
C is always invisible, A and B visibility depends on C

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

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

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...