Odoo many2one field related to one2many - odoo

I'm confused by something in the Odoo source code. On the stock.picking model, there is a product_id field. It's defined as a related field via move_lines.product_id.
move_lines is a one2many field. I don't understand how a many2one field can use a one2many field as its relation.
Here's a link to the source code I'm referring to:
https://github.com/odoo/odoo/blob/316ffc80147de076b28c6156ac679dd90da0935e/addons/stock/models/stock_picking.py#L288
You can see that product_id is defined as:
product_id = fields.Many2one('product.product', 'Product', related='move_lines.product_id')
And move_lines is defined as:
move_lines = fields.One2many('stock.move', 'picking_id', string="Stock Moves", copy=True)
What is the purpose of this definition? How is it even allowed?
If I look at the value of the product_id field for a picking, it returns the product for the first move line in the picking, not all of products.
However, if I search the picking tree view with a custom filter on the Product field, for example, Product contains 'Product Name', the results seem to account for all products in the picking. If I search for any product in the picking the picking appears in the view, it's not just limited to the first product.
Can someone explain this behavior? There is even a note in the source code that the product_id field is specifically for searching, so I'm thinking there is some magic functionality I never knew about.

It's not related to the One2many field, it's related to the move_lines object (which is stock.move model), and takes from that model product_id field, which has a type of Many2one. So, everything is correct. Here's the code.

Related

Carry page values to another model page, Odoo

I want to carry notebook page One2many field to another model page.
These field from my main model. I want to carry page field with this operation.
T
How can fill these page fields after creating method.
Brother, Please state your question clearly.
I hope that notebook has a one2many relation with the purchase ?
If so, Before that you have to know in which purchase order id you want to add those field values.
Then create a object button in your custom model, use create method in that and in the dictionary pass the same purchase order id in that.

Odoo 15 Find tax_ids Inside the account.move.line (Invoice) Model

Good day, hope everything's well.
I'm trying to find the value of tax_ids (Many2many field) inside the account.move.line model but i can't seems to find anything. I already access the psql of the database but i cant find tax_ids too.
I accessed that account.move.line model with ORM like this :
def _post(self, soft=True):
for move in self:
....
account_move_line = self.env['account.move.line'].search([('id', '=', int(move.id))])
print(account_move_line.tax_ids) #this find nothing
could someone please elaborate how is it possible to access id of the tax that applied to, in this case, an invoice line record?
Edit : Sometimes this ORM fetching the ID and sometimes it doesn't. But most likely it's not fetching.
I'm trying to find the value of tax_ids (Many2many field) inside the
account.move.line model but i can't seems to find anything. I already
access the psql of the database but i cant find tax_ids too.
tax_ids in account.move.line model is a Many2Many field, which is stored separately as another table in the database. This kind of relation field mostly be named something like this (main_table)_(related_table)_rel (ignore the parentheses). For example, this particular case's table is account_move_line_account_tax_rel since its main table is account_move_line and the related table for that specific field is account_tax. Inside the relation table, you will almost always find 2 fields mapping both ids together. For this case, it is going to be account_move_line_id and account_tax_id.
I accessed that account.move.line model with ORM like this :
def _post(self, soft=True):
for move in self:
....
account_move_line = self.env['account.move.line'].search([('id', '=', int(move.id))])
print(account_move_line.tax_ids) #this find nothing could someone please elaborate how is it possible to access id of the tax
that applied to, in this case, an invoice line record?
Edit : Sometimes this ORM fetching the ID and sometimes it doesn't.
But most likely it's not fetching.
Accessing a field via ORM always works as you intended if it is implemented correctly. In your code, you are searching account_move_line by id but you are trying to search it with move.id, which I assume it is account_move since you got the data sometimes. If you can access the lines that you want correctly, you will see that account_move_line.tax_ids will always give you the correct tax_ids. From what your code looks, I think you are trying to search the lines by its account_move. Therefore, your domain should be [('move_id', '=', int(move.id))] instead.

Compute many2many field with values from another model using Odoo developer interface

I need a new field inside Contact model that would hold information about Allowed companies of the related user.
Now there is only field about Currently picked company by that user (and it is not enough for me to make a record rule).
The field I want to copy values from is inside model Users and it is called company_ids.
I’m trying to add this field in the developer mode (Settings > Technical > Fields) like this:
But I’m having trouble with code that would fill my field with values from the another model.
for record in self:
record[("x_company_ids")] = env['res.users'].company_ids
I’m guessing that the record is referring to a record inside Contact model and it does not contain fields from another models like Users. So I can’t figure it out how to reference a field from another model.
Something similar to this: env['res.users'].company_ids?
It is even harder for me because it is many2many field and should always update when the source changes.
Maybe better solution would be to use Automatic action to write values to this field?
I saw some threads like this: Computed many2many field dependencies in Odoo 10.
But it seems like in those cases they had clear connection between the fields and I don't have it. I don't know how to get related user while I'm inside Contact model. I know only how to this oposite way (from user to contact): user.partner_id.id
Here in below given code you haven't specified related user from which you will get company_ids, you have directly accessing company_ids
for record in self:
record[("x_company_ids")] = env['res.users'].company_ids
You can write as following :
for record in self:
record["x_company_ids"] = self.env['res.users'].search([('partner_id','=',record.id)]).company_ids

How to add a many2one relation to a custom model

I try to add to an order a new attribute that relates to a custom many2one relation. The goal is to choose for each order one specific contract condition. I would like to manage those contract conditions in the database, so that I can easily manage them.
I sort of got far. I can edit those conditions, assign them and get them properly printed. However, on the sale-order form they get displayed in a weird way. Instead of the descirption-text of the condition, I see sort of a description of the associated record. So my question is, how to show the proper description attribute. See here:
Below I added a few screenshots that explain the type of changes that I did.
custom data structure:
many2one relation from sale.order to custom structure:
views for custom structure:
reference from order form, which is displayed oddly
Define _rec_name into your class.
_rec_name = 'x_condition'
It's because it will looking for name field into your custom model when you add many2one field for that model, when you define _rec_name it will take that field value.
Try to use x_name instead of x_condition for field name

OpenERP - Add fields only to some products inside a Category

I'm setting up OpenERP v7. I know how to create modules and so on.
The problem is that not all the products will have the same fields. For example for T-shirts I would have Color, Size. But for belts I will have: Material, Length (as an example).
I know the option of using multi-variants but it's very very confusing. It creates a new column for Dimension values but then the Name of the product has also the Dimension values in it. It's very annoying.
Also, the access to the Product Templates is only accessible from the Sales module, so our Procurement department doesn't have access to it.
I would like more if a pre-defined template appears after a user selects the category of the product. Since products in same category will have the same field requirements.
I would suggest you the following.
Define all fields in your model (color, size, material etc.). Then hide the from views according to the product category.
To hide some field based on the category_id value you may try something like this:
<field name="some_field"
attrs="{'invisible': [('category_id', 'not in', [1, 2, 3])]}"/>
I found out a better way to solve it, which is by using the module product_custom_attributes, downloadable from: https://www.odoo.com/apps/7.0/product_custom_attributes/
This module let's me create sets of attributes that can be later loaded into the product, I can also filtrate the products depending on the attribute value which is cool.