Odoo show values of selected Many2one record - orm

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

Related

Odoo can I display a field of a many2one related field without duplicating it in the model using the related flag

Say, in odoo I have a Model A with a many2one field to model B.
Model B has a field "city".
Now I want to create a form for model A in which I want the "city" field of model B.
I can do this by adding the city field of Model B to model A as well and giving it the related flag.
b_city = fields.Char(related='b_id.city', depends=['b_id'])
But I dont like this because than I have to add this field to my model. I would prefer if can do this without creating this field. Is this possible?
-----------Edit---------------
Something that I'm looking for is like this:
<page string="Offers">
<field name="offer_ids">
<tree>
<field name="price"/>
<field name="partner_id"/>
<field name="validity"/>
<field name="date_deadline"/>
<button name="accept" type="object" icon="fa-check"/>
<button name="reject" string="X" type="object" icon="fa-xmark"/>
<field name="status"/>
</tree>
</field>
</page>
This a page in a form where the corresponding model has a one2many field which is being displayed in a tree view. Now, I want to the other way around. I want to display in a form some fields of another model to which the there is a many2one field.
Is it possible to do this in the the xml view?
There is no way to do that in Odoo unless using related field.
Check Odoo ORM documentation
USING PYTHON CODE :
Example 1:
invoice_partner_icon = fields.Char(compute='_compute_invoice_partner_display_info', store=False, compute_sudo=True)
#api.depends('partner_id', 'invoice_source_email')
def _compute_invoice_partner_display_info(self):
for move in self:
vendor_display_name = move.partner_id.display_name
if not vendor_display_name:
if move.invoice_source_email:
vendor_display_name = _('From: ') + move.invoice_source_email
move.invoice_partner_icon = '#'
else:
vendor_display_name = _('Created by: %s') % (move.sudo().create_uid.name or self.env.user.name)
move.invoice_partner_icon = '#'
else:
move.invoice_partner_icon = False
move.invoice_partner_display_name = vendor_display_name
OR USING UI :
As admin, You can create a new field in Model B which is related to model A without storing it in the database : in App Settings, switch to debug mode by adding "?debug=1" in your url, reload the page, then go to the new Technical Menu-tab > Fields.
Then, create your new related field using the panel "Advanced Properties" Related Field: ...
and then uncheck [ ] Stored

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.

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