Ticket #6406: form_save_hooks.diff

File form_save_hooks.diff, 2.5 KB (added by Alex Gaynor, 17 years ago)

Ok, this is a fairly simple implementation, if this is not what was intended let me know so I can fix it, fair simple/

  • django/contrib/admin/options.py

    diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
    index d0cca0a..ad8ff94 100644
    a b class ModelAdmin(BaseModelAdmin):  
    329329        from django.contrib.admin.models import LogEntry, ADDITION
    330330        from django.contrib.contenttypes.models import ContentType
    331331        opts = model._meta
    332         new_object = form.save(commit=True)
    333 
    334         if formsets:
    335             for formset in formsets:
    336                 # HACK: it seems like the parent obejct should be passed into
    337                 # a method of something, not just set as an attribute
    338                 formset.instance = new_object
    339                 formset.save()
     332        new_object = self.save_add_form(request=request, model=model, form=form, formsets=formsets)
    340333
    341334        pk_value = new_object._get_pk_val()
    342335        LogEntry.objects.log_action(request.user.id, ContentType.objects.get_for_model(model).id, pk_value, force_unicode(new_object), ADDITION)
    class ModelAdmin(BaseModelAdmin):  
    378371        from django.contrib.admin.models import LogEntry, CHANGE
    379372        from django.contrib.contenttypes.models import ContentType
    380373        opts = model._meta
    381         new_object = form.save(commit=True)
     374        new_object = self.save_change_form(request=request, model=model, form=form, formsets=formsets)
    382375        pk_value = new_object._get_pk_val()
    383376
    384         if formsets:
    385             for formset in formsets:
    386                 formset.save()
    387377
    388378        # Construct the change message. TODO: Temporarily commented-out,
    389379        # as manipulator object doesn't exist anymore, and we don't yet
    class ModelAdmin(BaseModelAdmin):  
    417407            request.user.message_set.create(message=msg)
    418408            return HttpResponseRedirect("../")
    419409    save_change = transaction.commit_on_success(save_change)
     410   
     411    def save_add_form(self, request, model, form, formsets):
     412        new_object = form.save(commit=True)
     413       
     414        if formsets:
     415            for formset in formsets:
     416                formset.instance = new_object
     417                formset.save()
     418       
     419        return new_object
     420       
     421    def save_change_form(self, request, model, form, formsets):
     422        new_object = form.save(commit=True)
     423       
     424        if formsets:
     425            for formset in formsets:
     426                formset.save()
     427       
     428        return new_object
    420429
    421430    def render_change_form(self, request, model, context, add=False, change=False, form_url='', obj=None):
    422431        opts = model._meta
Back to Top