This question has been flagged

I am trying to inherit a method in stock.inventory.line. My issue is, when this method is called from write method, it is working fine (inherited function work), but if I call it from create method, the original function is being used and my inherited function is being ignored.

Original method:

def _check_no_duplicate_line(self):
domain = [
('product_id', 'in', self.product_id.ids),
('location_id', 'in', self.location_id.ids),
'|', ('partner_id', 'in', self.partner_id.ids), ('partner_id', '=', None),
'|', ('package_id', 'in', self.package_id.ids), ('package_id', '=', None),
'|', ('prod_lot_id', 'in', self.prod_lot_id.ids), ('prod_lot_id', '=', None),
'|', ('inventory_id', 'in', self.inventory_id.ids), ('inventory_id', '=', None),
]
groupby_fields = ['product_id', 'location_id', 'partner_id', 'package_id', 'prod_lot_id', 'inventory_id']
lines_count = {}
for group in self.read_group(domain, ['product_id'], groupby_fields, lazy=False):
key = tuple([group[field] and group[field][0] for field in groupby_fields])
lines_count[key] = group['__count']
for line in self:
key = (line.product_id.id, line.location_id.id, line.partner_id.id, line.package_id.id, line.prod_lot_id.id, line.inventory_id.id)
if lines_count[key] > 1:
raise UserError(_("There is already one inventory adjustment line for this product,"
" you should rather modify this one instead of creating a new one."))


Inherited method:


    def _check_no_duplicate_line(self):
res = super(InventoryLine, self)._check_no_duplicate_line()
print('duplicate line called')
_logger.debug('duplicate line verifeid')
# active_id = self.env.context.get('active_id')
# print(active_id)
invent = self.inventory_id.my_custom_field
print(invent)
if invent:
print('in duplicate tag id fond')
return
else:
print('no id found')
return res

write method:

def write(self, vals):
res = super(InventoryLine, self).write(vals)
self._check_no_duplicate_line()
return res

create method:

@api.model_create_multi
def create(self, vals_list):
some code here. . .

res = super(InventoryLine, self).create(vals_list)
res._check_no_duplicate_line()
return res

What could be the reason? Any help will be greatly appreciated.

Avatar
Discard
Best Answer

Hi, 

I'm commenting your code generally speaking: 

1- In create function, you're calling bulk create.Therefore, You may have a recordset that's been created. so, you need to loop inside your method.

Using return inside of a loop will break it and exit the function even if the loop is still not finished.

Can you try this instead: 

def _check_no_duplicate_line(self):
    for line in self:
        if not line.inventory_id.my_custom_field:
            super(InventoryLine, line)._check_no_duplicate_line()

Add prints or pdb debug to check.

You can write back for further analysis.
Regards.

Avatar
Discard
Author

Thank you for your response.
I tried what you suggested.
I added _logger.debug('called successfully') in your proposed code (loop)
when I called it from write function, I am getting log in terminal "called successfully"
but when I call it for create, I am not even getting this log, but I am getting error in original method. It is really upsetting me.

can you debug code using pdb package and put it (import pdb; pdb.set_trace() ) just after calling super of create function please?

Then print res and click on "s" in order to get into your method...etc
this is how we're going to understand what's happening.
more at: https://docs.python.org/3/library/pdb.html#pdbcommand-next
--------------------------
If all this is "useless", i suggest you use monkey patch : targetting your method in import section, declare all the method and then replace the native with yours.

Author

Thank you very much for pointing me the right direction. I don't know what is the issue, but I just tested my custom module on another installation after applying your proposed solution (loop), and it worked fine. I did several tests there. It is working fine, but in old installation, it is still the same. Anyway, +100 for you. I will test it with pdb and post results here later from my effected installation.