ORM search dynamically? Odoo13 - odoo

I've made a model with a field name Car where users can write a car model, so I want to count the number of each model.
I was looking
self.env['car.model'].search_count(['car_model', '=', 'name_of_model'])
So I want to count every data that user write but dynamically.

Related

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 order rows when custom model is made from GUI in Odoo v 10?

I have made a custom model using GUI. This model contains a field named sequence.
I want to order my rows by this field.
This same this has been done for res.company model and i want the same.
So, for companies
_order = 'sequence, name' is written in model.
and widget='handle' is used in tree view that orders various companies just by drag and drop.
So, i added this widget attribute in my tree view, but i don't know where to use _order='sequence'(the field I created in my model) because i don't have code for that model.
Is there a way that i can do i from GUI itself or I need to create that model from code??

Getting whole object from Odoo relation via API

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

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.

Django queries: how to annotate with a filtered count?

Suppose I have a Book model with a language field and a foreign key to a Publisher model.
Currently I use a Count annotation in a custom Publisher manager to allow me to add to the admin a sortable column with the number of books by each publisher. (see How to add a sortable count column to the Django admin of a model with a many-to-one relation? )
My problem now is that I need to have a different column count for the books published in each language.
Is there any way to make the annotation subject to a filter of the related model?
You can use the double underscore __ for this purpose. Something like this (snippet taken from question linked to by OP):
class PublisherManager(models.Manager):
def get_query_set(self):
return super(PublisherManager,self).get_query_set().annotate(lang_count=Count('book__language'))