We have a third party lib that we use in our Django 1.9 app. We would like to amend that application with some functionality not in the original app (targeting MongoDB). We currently do this via our own fork of the original lib, but would like to make the changes a bit more orthogonal, so that they might be pulled upstream eventually.
We've tried patching during the app config ready()
but model imports are handled before this call in django.apps.registry.populate()
, and performing it in the \__init__
suffers from apps_ready == False
. What is the best part of the lifecycle to perform this?
class MongoConfig(AppConfig):
def __init__(self, app_name, app_module):
super(MongoConfig, self).__init__(app_name, app_module)
for p in patches:
patch(*p)
def patch(old, new):
old_module, old_item = split_mod(old)
new_module, new_item = split_mod(new)
print('patching {0} with {1}'.format(old, new))
old_module = import_module(old_module)
new_module = import_module(new_module)
setattr(old_module, old_item, getattr(new_module, new_item))