odoo實(shí)現(xiàn)產(chǎn)品分類自動(dòng)生成二級(jí)菜單

需求:之前做過(guò)的產(chǎn)品查詢系統(tǒng),需要有很多產(chǎn)品分類。odoo的菜單無(wú)法將每個(gè)分類自動(dòng)顯示在菜單上,所以只能手動(dòng)創(chuàng)建與產(chǎn)品分類名稱相同的二級(jí)菜單綁定動(dòng)作。無(wú)法實(shí)現(xiàn)自動(dòng)化和通用化。

思路:我的理解,odoo一切都是記錄,菜單也是存到數(shù)據(jù)庫(kù)的記錄

創(chuàng)建分類的時(shí)候 同步創(chuàng)建動(dòng)作和菜單 ,廢話不多說(shuō),上代碼:

    name = fields.Char(string='Name')
    parent_id = fields.Many2one('product.search.category', string='Parent Category')
    chlid_ids = fields.One2many('product.search.category', 'parent_id', string='Chlid Cat')

    
    @api.model
    def create(self, values):
        res = super(ProductSearchCategory, self).create(values)
        res._create_menu()
        return res

    def _create_menu(self):
        action = self._create_action()
        parent_id = self.env.ref('product_search.menu_product_search_list').id
        menu_model = self.env['ir.ui.menu']
        if self.parent_id:
            parent = menu_model.search([
                ('name', '=', '%s_menu' % self.parent_id.name)
            ])
            if parent:
                parent_id = parent[0].id
            else:
                raise UserError(u'不存在父級(jí)分類的菜單!')
        menu_model.create({
            'name': '%s_menu' % self.name,
            'action': '%s,%s' % (action._name, action.id),
            'parent_id': parent_id
        })

    def _create_action(self):
        action = self.env['ir.actions.act_window'].create({
            'name': '%s_action' % self.name,
            'res_model': 'product.search.product',
            'context':{'defaut_category_id': self.id},
            'domain': [('category_id', '=', self.id)],
        })
        return action 
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容