Ticket #8005: 8005.3.diff

File 8005.3.diff, 1.7 KB (added by simon, 16 years ago)

Patch illustrating different approach, using pre_save and post_save hooks

  • django/contrib/admin/options.py

     
    325325        for inline in self.inline_instances:
    326326            yield inline.get_formset(request, obj)
    327327
     328    def pre_save(self, request, instance, is_add):
     329        pass
     330   
     331    def post_save(self, request, instance, is_add):
     332        pass
     333
    328334    def save_add(self, request, form, formsets, post_url_continue):
    329335        """
    330336        Saves the object in the "add" stage and returns an HttpResponseRedirect.
     
    333339        """
    334340        from django.contrib.admin.models import LogEntry, ADDITION
    335341        opts = self.model._meta
    336         new_object = form.save(commit=True)
    337 
     342       
     343        new_object = form.save(commit=False)
     344        self.pre_save(request, new_object, True)
     345        new_object.save()
     346        if hasattr(new_object, 'save_m2m'):
     347            new_object.save_m2m()
     348        self.post_save(request, new_object, True)
     349       
    338350        if formsets:
    339351            for formset in formsets:
    340352                # HACK: it seems like the parent obejct should be passed into
     
    382394        """
    383395        from django.contrib.admin.models import LogEntry, CHANGE
    384396        opts = self.model._meta
    385         new_object = form.save(commit=True)
     397       
     398        new_object = form.save(commit=False)
     399        self.pre_save(request, new_object, False)
     400        new_object.save()
     401        if hasattr(new_object, 'save_m2m'):
     402            new_object.save_m2m()
     403        self.post_save(request, new_object, False)
     404       
    386405        pk_value = new_object._get_pk_val()
    387406
    388407        if formsets:
Back to Top