PreSaveMiddleware

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
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)

More like this

  1. "Autoconnect" model decorator, easy pre_save and post_save signal connection by bendavis78 3 years, 9 months ago
  2. Accept Header Middleware by kioopi 6 years ago
  3. URL models by diverman 4 years, 6 months ago
  4. Add get_addr() method to request object by nikmolnar 1 year, 4 months ago
  5. Format transition middleware by limodou 7 years, 1 month ago

Comments

(Forgotten your password?)