'How to override the unlink method of 'res.partner' model? Odoo 14
I want to remove the condition that generates the UserError in unlink method of 'res.partner' model:
def unlink(self):
running_sessions = self.env['pos.session'].sudo().search([('state', '!=', 'closed')])
if running_sessions:
raise UserError(
_("You cannot delete contacts while there are active PoS sessions. Close the session(s) %s first.")
% ", ".join(session.name for session in running_sessions)
)
return super(ResPartner, self).unlink()
I try by this code , but it always execute the standard function :
class ResPartner(models.Model):
_inherit = 'res.partner'
def unlink(self):
running_sessions = self.env['pos.session'].sudo().search(
[('state', 'not in', ['opening_control', 'opened', 'closing_control', 'closed'])])
if running_sessions:
raise UserError(
_("You cannot delete contacts while there are active PoS sessions. Close the session(s) %s first.")
% ", ".join(session.name for session in running_sessions)
)
else:
return ResPartner.unlink(self)
How can I do it ? Any help please?
Solution 1:[1]
Your code here is correct
I try by this code , but it always execute the standard function :
class ResPartner(models.Model):
_inherit = 'res.partner'
def unlink(self):
return super(ResPartner, self).unlink()
And you ask it to execute the standard method.
--- Edit ---
After re-reading question, I understood that you want to overwrite integrity check that point of sale module implements. This isn't good idea. You should obey that restriction. There is a good reason for that.
Instead, mark partner inactive or add a column to mark partner to be removed when conditions allows you to do it.
for example
class ResPartner(models.Model):
_inherit = 'res.partner'
remove_me = fields.Boolean('Marked for removal')
def unlink(self):
try:
return super(ResPartner, self).unlink()
except UserError:
# If partner cannot be removed mark it as inactive and for removal
self.remove_me = True
self.active = False
And then create some scheduled action to remove partners marked for removal. Or leave them in database as inactive. You cannot remove a res.partner object anyway if there is any other object which has reference to it. And removing data from ERP systems is always a bit suspicious.
Solution 2:[2]
You can use @api.model
annotation and the super
method.
That's all, you can find example if search for @api.model
in original odoo directory
Solution 3:[3]
Try below example of code to replace unlink method of specific file.
from odoo.addons.point_of_sale.models.res_partner import ResPartner
def unlink(self):
running_sessions = self.env['pos.session'].sudo().search([('state', '!=', 'closed')])
if running_sessions:
pass
return super(ResPartner, self).unlink()
ResPartner.unlink = unlink
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | RapThor |
Solution 3 | Siddharth Tarpada |