How to override method in frappe?

Is this different to this existing hook?

override_whitelisted_methods

That only works for whitelisted methods, which are those methods designated accessible to javascript.

@netchampfaris what about the method not belong to a class?

Like this file frappe/frappe/utils/data.py

1 Like

Other form that you can use to override methods in an application is call build_my_thing function at the end of hooks.py file. Specially userful when the method is in a class controller. We are testing that solution but apparently it works.

2 Likes

how to override the python method (not a whitelisted method and also not a method inside the class) which is in the erpnext app @rmehta @netchampfaris @revant_one @Mohammed_Redha @peterg

Did you check below before tagging to so many people ? What you have done so far and what are you trying to do ?

@sanjay yes I tried, I can able to override the whitelisted erpnext method from custom app via hooks.py and also a method inside the class, but i could not able to override the python method

Were you able to achieve it? I’m using override_doctype_class of hooks but it’s not working…

Hi @netchampfaris I need to use override_doctype_class hook but not sure if I’m missing something because it’s not working for me. The method metioned in the override_doctype_class is not reached. Could you please help?

I think override_doctype_class hook is part of v13 and above.

1 Like

@revant_one @netchampfaris Is there any way to override the method not belong to a class?

2 Likes

You can refer below :

2 Likes

any updates for version 13.

lifesaver! Exactly what I was looking for. I needed to disable the amounts in CoA as it’s using too much computing resource for very big transactions, when we press expand all.

I used the above method (by revant_one) to override the validate method of EmployeeCheckin doctype. It worked perfectly.

But, something else broke. I have a custom script (JS) on after_save of EmployeeCheckin. This code isn’t executed anymore. Can anyone explain why this happens? How do I get some front end code running on after_save?

I was once told (i don’t remember in what thread) that after_save hook will be triggered no matter how the doc is inserted/saved.
So if it is not (as is my case) it might be a bug. Report it on the github.

refer to this app GitHub - aakvatech/CSF_TZ for monkey patching any method

  1. copy and adapt this file into your own custom app
    CSF_TZ/__init__.py at master · aakvatech/CSF_TZ · GitHub
  2. create python file under monkey_patches folder like this file.
    CSF_TZ/db_transaction_writes.py at master · aakvatech/CSF_TZ · GitHub
5 Likes

How to override python method which isn’t inside class?

for example, get_leaves_for_period method inside Leave Application package?

I tried:

myapp/myapp/hr/leave_application.py

from erpnext.hr.doctype.leave_application import leave_application

def get_leaves_for_period_new(employee, leave_type, from_date, to_date, do_not_skip_expired_leaves=False):
    frappe.throw(_("hello"))


def override(doc=None, target=None):
    leave_application.get_pending_leaves_for_period = get_pending_leaves_for_period_new


then in hooks.py

doc_events = {
    "Leave Application": {

        "validate": "myapp.myapp.hr.leave_application.override",

        "onload": "myapp.myapp.hr.leave_application.override",

        "refresh": "myapp.myapp.hr.leave_application.override"

    }

and this approach not working.

did you try this? Hooks

ye, but this is about overriding doctype class, but method get_pending_leaves_for_period inside leave_application is not a class method, it’s a method which is inside leave_application.py but not in Leave Application class.