Skip to Content
Menu
This question has been flagged

I want to modify tree attributes inside form view for each record. I tried to use get_view() but it is not working as expected and active_id is not the one I opened and got another record id. Which function is working everytime we open the record form view and how can I do it? This is my sample code for adding limit attribute to tree.


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


Avatar
Discard
Related Posts Replies Views Activity
1
Feb 24
2708
2
Aug 24
2842
2
Jan 24
935
1
Nov 23
1319
2
Jun 23
3830