odoo16中如何修改表单视图内的树属性?

问题描述 投票:0回答:1

我想修改每个记录的表单视图内的树属性。我尝试使用 get_view() 但它没有按预期工作,并且 active_id 不是我打开并获得另一条记录的那个。每次打开记录表单视图时哪个功能在起作用?我该怎么做?这是我向树添加限制属性的示例代码。

from odoo import models, fields, api
from lxml import etree
import logging

class StockPicking(models.Model):
    _inherit = "stock.picking"

    limit = fields.Integer(string="Tree Pagination Limit")

    def tree_pagination_limit_apply(self):
        return {
            'type': 'ir.actions.client',
            'tag': 'reload',
        }

    @api.model
    def get_view(self, view_id=None, view_type='form', **options):
        logging.info("Custom get_view called")

        # Fetch the original view
        result = super(StockPicking, self).get_view(view_id=view_id, view_type=view_type, **options)

        if view_type == 'form':
            # Parse the view architecture
            doc = etree.XML(result['arch'])

            # Locate the specific tree view within the form
            for tree in doc.xpath("//field[@name='move_ids_without_package']/tree"):
                # Ensure the context has 'active_id' when this form is opened
                if 'active_id' in self.env.context:
                    active_id = self.env.context.get('active_id')

                    # Ensure active_id is not None and browse the record
                    if active_id:
                        try:
                            current_record = self.browse(active_id)
                            if current_record and current_record.limit > 0:
                                tree.set('limit', str(current_record.limit))
                                logging.info(f"Set tree view limit to {current_record.limit}")
                        except Exception as e:
                            logging.error(f"Error setting limit on tree view: {e}")

            # Update the architecture in the result
            result['arch'] = etree.tostring(doc, encoding='unicode')

        return result

python xml odoo odoo-16
1个回答
0
投票

您可以从上下文中获取记录 ID

params

尝试更换:

if 'active_id' in self.env.context:
    active_id = self.env.context.get('active_id')

与:

if 'params' in self.env.context:
    active_id = self.env.context['params'].get('id')
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.