from django.dispatch import dispatcher
from django.db.models import signals
PRE_SAVE_REQUEST = None
class PreSaveMiddleware(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
"""
def process_request(self, request):
global PRE_SAVE_REQUEST
PRE_SAVE_REQUEST = request
def pre_save(**kwargs):
instance = kwargs['instance']
if getattr(instance, 'pre_save', None):
instance.pre_save(PRE_SAVE_REQUEST)
dispatcher.connect(pre_save, signal=signals.pre_save)
Comments