تخطي للذهاب إلى المحتوى
القائمة
لقد تم الإبلاغ عن هذا السؤال
1 الرد
1700 أدوات العرض

I want to override/extend the execute_import from the model base_import.import.

Base Code:

class Import(models.TransientModel):
​_name = 'base_import.import'

​def execute_import(self, fields, columns, options, dryrun=False):
​//some code
import_result = model.load(import_fields, merged_data)
​return import_result​

From the above code, i want to override and do some of my own logic using 'import_result'.

Here's what I've done(Inherited code):

class Import(models.TransientModel):
​_inherit = 'base_import.import

​def execute_import(self, fields, columns, options, dryrun=False):
​res_import = super(Import, self).execute_import(fields, columns, options, dryrun)
​// here i need to use import_result values
result = import_result.get('ids')​

How to access import_result in inherited method.


Thank you


الصورة الرمزية
إهمال
أفضل إجابة

To override the execute_import method and use the import_result in your inherited method, you need to ensure that you correctly call the super method and then perform your additional logic using the import_result returned by the super method.

Here's how you can achieve that:

class Import(models.TransientModel):
    _inherit = 'base_import.import'

    def execute_import(self, fields, columns, options, dryrun=False):
        # Call the super method and store the result
        import_result = super(Import, self).execute_import(fields, columns, options, dryrun)
        
        # Perform your custom logic with the import_result
        if 'ids' in import_result:
            result_ids = import_result['ids']
            # Your custom logic using result_ids
            # For example, log the imported IDs
            _logger.info(f"Imported IDs: {result_ids}")
        
        # Return the result of the import
        return import_result


الصورة الرمزية
إهمال
المنشورات ذات الصلة الردود أدوات العرض النشاط
2
مايو 25
1013
4
يوليو 25
1525
1
يوليو 25
787
4
مايو 25
4671
1
أبريل 25
843