[Django]-How to monkey patch django function?

2👍

Here you override the local variable dispaly_for_field. If you want to override it, you need to set the attribute of the module:

from django.contrib.admin import utils
from warehouse.monkey_patching import _display_for_field_mod

utils.display_for_field = _display_for_field_mod

As you probably already know, you must make sure that you monkey patch the function before other modules import it, since otherwise, these will obtain a reference to the “old” function. You thus probably should import this as (one of the) first lines in your manage.py file.

Leave a comment