Code

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

File model-admin-save-hooks.diff, 2.6 KB (added by Petr Marhoun <petr.marhoun@…>, 7 years ago)
Line 
1=== modified file 'django/contrib/admin/options.py'
2--- django/contrib/admin/options.py     2007-09-10 00:27:48 +0000
3+++ django/contrib/admin/options.py     2007-09-11 14:41:26 +0000
4@@ -318,14 +318,8 @@
5         from django.contrib.admin.models import LogEntry, ADDITION
6         from django.contrib.contenttypes.models import ContentType
7         opts = model._meta
8-        new_object = form.save(commit=True)
9-
10-        if formsets:
11-            for formset in formsets:
12-                # HACK: it seems like the parent obejct should be passed into
13-                # a method of something, not just set as an attribute
14-                formset.instance = new_object
15-                formset.save()
16+       
17+        new_object = self.form_save_add(request, model, form, formsets)
18 
19         pk_value = new_object._get_pk_val()
20         LogEntry.objects.log_action(request.user.id, ContentType.objects.get_for_model(model).id, pk_value, str(new_object), ADDITION)
21@@ -367,13 +361,11 @@
22         from django.contrib.admin.models import LogEntry, CHANGE
23         from django.contrib.contenttypes.models import ContentType
24         opts = model._meta
25-        new_object = form.save(commit=True)
26+
27+        new_object = self.form_save_change(request, model, form, formsets)
28+
29         pk_value = new_object._get_pk_val()
30 
31-        if formsets:
32-            for formset in formsets:
33-                formset.save()
34-
35         # Construct the change message. TODO: Temporarily commented-out,
36         # as manipulator object doesn't exist anymore, and we don't yet
37         # have a way to get fields_added, fields_changed, fields_deleted.
38@@ -406,6 +398,23 @@
39             request.user.message_set.create(message=msg)
40             return HttpResponseRedirect("../")
41 
42+    def form_save_add(self, request, model, form, formsets, commit=True):
43+        return self.form_save(request, model, form, formsets, commit, True)
44+
45+    def form_save_change(self, request, model, form, formsets, commit=True):
46+        return self.form_save(request, model, form, formsets, commit, False)
47+
48+    def form_save(self, request, model, form, formsets, commit, add):
49+        object = form.save(commit=commit)
50+        if formsets:
51+            for formset in formsets:
52+                if add:
53+                    # HACK: it seems like the parent object should be passed
54+                    # into a method of something, not just set as an attribute
55+                    formset.instance = object
56+                formset.save()
57+        return object
58+
59     def add_view(self, request, form_url=''):
60         "The 'add' admin view for this model."
61         from django.contrib.admin.views.main import render_change_form
62