fromdjango.dispatchimportdispatcherfromdjango.db.modelsimportsignalsPRE_SAVE_REQUEST=NoneclassPreSaveMiddleware(object):""" This bit of middleware just saves the request in a global variable that can be used to pass the request on to any pre_save methods in your models """defprocess_request(self,request):globalPRE_SAVE_REQUESTPRE_SAVE_REQUEST=requestdefpre_save(**kwargs):instance=kwargs['instance']ifgetattr(instance,'pre_save',None):instance.pre_save(PRE_SAVE_REQUEST)dispatcher.connect(pre_save,signal=signals.pre_save)