Odoo: how to search a parent_id and its all child in product.category

我与影子孤独终老i 提交于 2021-02-11 12:41:52

问题


I want to, when the user selected (through Many2one field) a category, I need to find its related parent_id for Brands to add a domain filter on another Many2one field to have related child entries of these Brands. As I am beginner I failed to how to do this.

Suppose Many2one field category_id here, user will select a category from first three entries, now we know that user wants to add an Electronics related item then system should populate Many2one field brand_id on Electronics related Brands (except All / Electronics / Brands) to select from below last 2 entries here in All / Electronics.

All / Electronics / Personal Computers
All / Electronics / Personal Computers / Laptop
All / Electronics / Personal Computers / Desktop
... 
...
All / Electronics / Brands
All / Electronics / Brands / HP
All / Electronics / Brands / IBM

There are more brands in this model (table data) like:

All / Foods / Brands
All / Foods / Brands / Hershey
All / Foods / Brands / Heinz
All / Foods / Brands / Oreo
...
All / Services / Brands
All / Services / Brands / IBM Garages
All / Services / Brands / Telos MNS
All / Services / Brands / Relia Quest
...

My custom Model class:

class OrderItems(models.Model):
    _name = 'tests.orderitems'
    _description = "Tests Order Items"

    store_id = fields.Many2one('tests.stores', string="Store", ondelete='cascade')
    order_id = fields.Many2one('tests.testsorders')
    categry_id = fields.Many2one('product.category', string="Category",
                                 domain="[['complete_name', 'not like', '%Brands%']]")
    items_id = fields.Many2one('tests.storeitems', string="Item",
                               domain="[['categs_id', '=', categry_id]]")
    brand_id = fields.Many2one('product.category', string="Brand",
                               domain="[['complete_name', 'like', '%Brands%']]")
    unit_id = fields.Many2one('tests.units', string="Unit")
    quantity = fields.Integer(string="Quantity", required=True, default=1)
    rates = fields.Float(string="Rate", default=0)
    size = fields.Char(string="Size")

Please help, how I can do it.


回答1:


@SDBot, the xml code for this particular model is here...

            <notebook>
                <page string="Order Items">
                    <field name="order_ids">
                        <tree editable="bottom" string="Order Items">
                            <field name="store_id"/>
                            <field name="categry_id"/>
                            <field name="items_id"/>
                            <field name="categ_name"/>
                            <field name="brand_id"/>
                            <field name="unit_id"/>
                            <field name="quantity"/>
                        </tree>
                    </field>
                </page>
            </notebook>

Model where added a field categ_name and a domain in brand_id

class OrderItems(models.Model):
    _name = 'tests.orderitems'
    _description = "Tests Order Items"

    store_id = fields.Many2one('tests.stores', string="Store", ondelete='cascade')
    order_id = fields.Many2one('tests.testsorders')
    categry_id = fields.Many2one('product.category', string="Category",
                                 domain="[['complete_name', 'not like', '%Brands%']]")
    items_id = fields.Many2one('tests.storeitems', string="Item",
                               domain="[['categs_id', '=', categry_id]]")
    categ_name = fields.Char('Category Name', related='categry_id.complete_name')
    brand_id = fields.Many2one('product.category', string="Brand",
                               domain="[('complete_name', 'like', categ_name),('complete_name', '!=', categ_name)]")
    unit_id = fields.Many2one('tests.units', string="Unit")
    quantity = fields.Integer(string="Quantity", required=True, default=1)
    rates = fields.Float(string="Rate", default=0)
    size = fields.Char(string="Size")



回答2:


Change your brand_id domain to:

categ_name = fields.char('Category Name', related='categry_id.complete_name')
brand_id = fields.Many2one('product.category', string="Brand",
                           domain="[('complete_name', 'like', categ_name),('complete_name', '!=', categ_name)]")

Here's the preview:



来源:https://stackoverflow.com/questions/65931511/odoo-how-to-search-a-parent-id-and-its-all-child-in-product-category

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!