Ticket #4507: model-admin-save-hooks.diff

File model-admin-save-hooks.diff, 2.6 KB (added by Petr Marhoun <petr.marhoun@…>, 17 years ago)
  • django/contrib/admin/options.py

    === modified file 'django/contrib/admin/options.py'
     
    318318        from django.contrib.admin.models import LogEntry, ADDITION
    319319        from django.contrib.contenttypes.models import ContentType
    320320        opts = model._meta
    321         new_object = form.save(commit=True)
    322 
    323         if formsets:
    324             for formset in formsets:
    325                 # HACK: it seems like the parent obejct should be passed into
    326                 # a method of something, not just set as an attribute
    327                 formset.instance = new_object
    328                 formset.save()
     321       
     322        new_object = self.form_save_add(request, model, form, formsets)
    329323
    330324        pk_value = new_object._get_pk_val()
    331325        LogEntry.objects.log_action(request.user.id, ContentType.objects.get_for_model(model).id, pk_value, str(new_object), ADDITION)
     
    367361        from django.contrib.admin.models import LogEntry, CHANGE
    368362        from django.contrib.contenttypes.models import ContentType
    369363        opts = model._meta
    370         new_object = form.save(commit=True)
     364
     365        new_object = self.form_save_change(request, model, form, formsets)
     366
    371367        pk_value = new_object._get_pk_val()
    372368
    373         if formsets:
    374             for formset in formsets:
    375                 formset.save()
    376 
    377369        # Construct the change message. TODO: Temporarily commented-out,
    378370        # as manipulator object doesn't exist anymore, and we don't yet
    379371        # have a way to get fields_added, fields_changed, fields_deleted.
     
    406398            request.user.message_set.create(message=msg)
    407399            return HttpResponseRedirect("../")
    408400
     401    def form_save_add(self, request, model, form, formsets, commit=True):
     402        return self.form_save(request, model, form, formsets, commit, True)
     403
     404    def form_save_change(self, request, model, form, formsets, commit=True):
     405        return self.form_save(request, model, form, formsets, commit, False)
     406
     407    def form_save(self, request, model, form, formsets, commit, add):
     408        object = form.save(commit=commit)
     409        if formsets:
     410            for formset in formsets:
     411                if add:
     412                    # HACK: it seems like the parent object should be passed
     413                    # into a method of something, not just set as an attribute
     414                    formset.instance = object
     415                formset.save()
     416        return object
     417
    409418    def add_view(self, request, form_url=''):
    410419        "The 'add' admin view for this model."
    411420        from django.contrib.admin.views.main import render_change_form
Back to Top