Getting whole object from Odoo relation via API - odoo

There are lots of Odoo models having relations. E.g. the res.partner model has a one2many relation called bank_ids where I can get bank account information for a customer. When I want to have them I need to get the customer (e.g. by ID) from the API and I have to get the res.partner.bank models depending on the values returned in the bank_ids field of the res.partner object with a second API call.
This is what I would like to prevent, a second API call. Is it possible to change the res.partner model so that I get the res.partner.bank objects instead of the IDs?

That's the standard recordset behavior. See https://www.odoo.com/documentation/8.0/reference/orm.html
Assuming you have a record called partner, you can access its banks with
for bank in partner.bank_ids:
# do your stuff with 'bank', like calling bank.name
where bank is a record of model res.partner.bank

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.

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 create just a view in odoo9 that does not save data in a model?

I am working in odoo9. Now I needed a view that permits the user to select partner and get his sale history.
Now I created a model "sale.history" but it saves the selected data as a record in db. I really don't need this.
How can I create a view for this.
Please also see this image.
You have two options for such views/reports.
Use TransientModel instead of Model for the model inheritance. Transient model records in database will be deleted by a frequently running cron job. The email message PopUp/Wizard is a nice example for that.
Write your own report (database view) for sales order. Actually there already is one report for that: Reporting/Sales/Sales Analysis. The model for that report is sale.report if you want to know, how it's done.
Aside from using a TransientModel (old api) or AbstractModel (new api)...you can simply set the store property of field to false, that way your field will never be persisted to the database, it will simply be a 'view field'.
class sale_history(model.Model):
_name='sale.history'
partner = fields.Many2one('res.partner', store=False)
The partner field will never get saved to the database
You can use store=False on the field in the model (as danidee suggested).
You can also overwrite the create method on the model.
Question - what is the purpose of the "sale.history" model? If it does not store any data at all then you may be better off creating a new view against "res.partner" rather than creating a new model.

Dynamic view models

If I'm about to split my application into read and write sides using CQRS principles how would you handle situation of having a "dynamic" read model scenario? E.g. I have a product that consists of several read properties (id, title, slug etc) but in order to display it to user I need to pass its price that is calculated by domain service (to which I pass visitor country and currency)? I can't store this price in database because user is free to change his currency anytime he wants + prices change quite often. So my final product view model should be basically a composite of raw-SQL columns (id, title, slug) and calculated price. Should I use the same view model that I get from the database and use content enrichment pattern or should I create a new view model for my composite?
Changing the price of the product is a COMMAND, it should update the price you store in the database which you then QUERY anytime.
The essence of CQRS is that your queries are not going any logic like that.
For currency conversion, since you say the user can change currency at anytime then you should query the entire list of currency rates and do the calculation yourself on the client side. Alternatively you could query for the base price then query for the current rate. And if the user changes currency you just query for the rate again but keep the same base price.

nested attributes without activerecord

I am trying to create an invoicing system that does not persist to a database, but passes through to a third party system. Hence, my models are not ActiveRecord as there is no table to persist to. How can I accept nested attributes for invoice line items in my invoice model?
This is an example of the resulting params from my form
{"payportal_invoice"=>{"invoice_lines"=>{"quantity"=>["1", "2"], "units"=>["hours", "hours"], "price"=>["10", "20"], "description"=>["desc", "d"]}, "dueDate"=>"10/10/2013", "invoiceNumber"=>"3.13.13.11:47", "description"=>"notes"}, "attachment_name"=>""}
I can easily create the invoice model from this params set, but am not sure how to create the line items
Have you tried using a tableless gem, such as https://github.com/softace/activerecord-tableless?
I've used them for similar situations in the past - you can do all of the standard validations, etc, but without needing to invoke ActiveRecord.