Giter Club home page Giter Club logo

Comments (9)

pedrobaeza avatar pedrobaeza commented on August 24, 2024

Probably... Usual optimization techniques are read_group or search_count.

from brand.

Rad0van avatar Rad0van commented on August 24, 2024

Probably... Usual optimization techniques are read_group or search_count.

Thanx for quick reply. Would you please care to elaborate a bit on how to apply the mentioned? The _compute_products_count only does this:

    @api.depends("product_ids")
    def _compute_products_count(self):
        for brand in self:
            brand.products_count = len(brand.product_ids)

I'd thought this would produce simple SQL query but obviously it is doing something more. I guess from the time it takes it somehow does a query for each Product with that Brand applied. Should I maybe use search_count in this method instead of what is there currently?

from brand.

pedrobaeza avatar pedrobaeza commented on August 24, 2024

It's caching all products data (more or less). You can do something similar (simpler in this case) to:

https://github.com/OCA/contract/blob/4db5c90a1d26c3ac5567f756e35839c4ee3d9492/contract/models/res_partner.py#L24-L39

from brand.

Rad0van avatar Rad0van commented on August 24, 2024

It's caching all products data (more or less). You can do something similar (simpler in this case) to:

https://github.com/OCA/contract/blob/4db5c90a1d26c3ac5567f756e35839c4ee3d9492/contract/models/res_partner.py#L24-L39

Yes but that's reading the objects and actually using them. AFAIK I do not need to load them just count them. So I guess I should use search_count in _compute_products_count implementation? Will try.

from brand.

Rad0van avatar Rad0van commented on August 24, 2024

Thanx @pedrobaeza The solution is quite simple. I create a module where I overrode the method to be like this:

class ProductBrand(models.Model):
    _inherit = "product.brand"

    @api.depends("product_ids")
    def _compute_products_count(self):
        contract_model = self.env['product.template']
        for brand in self:
            brand.products_count = contract_model.search_count([('product_brand_id', '=', brand.id)]) 

Is that correct?

from brand.

pedrobaeza avatar pedrobaeza commented on August 24, 2024

Yes, that's it

from brand.

Rad0van avatar Rad0van commented on August 24, 2024

Was asking just to be sure I do not miss anything. Does it make any sense to create PR and change the main module with this code?

from brand.

pedrobaeza avatar pedrobaeza commented on August 24, 2024

Yes, please go with the pr

from brand.

Rad0van avatar Rad0van commented on August 24, 2024

OK, will. Thanx for help, closing this one.

from brand.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.