Code

Ticket #4507: newforms-admin-form-save-hooks.diff

File newforms-admin-form-save-hooks.diff, 1.9 KB (added by Stas Shtin <antisvin@…>, 7 years ago)
Line 
1Index: django/contrib/admin/options.py
2===================================================================
3--- django/contrib/admin/options.py     (revision 5442)
4+++ django/contrib/admin/options.py     (working copy)
5@@ -292,6 +292,24 @@
6         """
7         return self.queryset(request)
8 
9+    def form_save(self, request, form):
10+        """
11+        Returns object saved from curent form.
12+        """
13+        return form.save(commit=True)
14+
15+    def form_save_add(self, request, form):
16+        """
17+        Returns object saved from current form in add stage.
18+        """
19+        return self.form_save(form, request)
20+
21+    def form_save_change(self, request, form):
22+        """
23+        Returns object saved from current form in change stage.
24+        """
25+        return self.form_save(form, request)
26+
27     def save_add(self, request, model, form, post_url_continue):
28         """
29         Saves the object in the "add" stage and returns an HttpResponseRedirect.
30@@ -301,7 +319,7 @@
31         from django.contrib.admin.models import LogEntry, ADDITION
32         from django.contrib.contenttypes.models import ContentType
33         opts = model._meta
34-        new_object = form.save(commit=True)
35+        new_object = self.form_save_add(request, form)
36         pk_value = new_object._get_pk_val()
37         LogEntry.objects.log_action(request.user.id, ContentType.objects.get_for_model(model).id, pk_value, str(new_object), ADDITION)
38         msg = _('The %(name)s "%(obj)s" was added successfully.') % {'name': opts.verbose_name, 'obj': new_object}
39@@ -340,7 +358,7 @@
40         from django.contrib.admin.models import LogEntry, CHANGE
41         from django.contrib.contenttypes.models import ContentType
42         opts = model._meta
43-        new_object = form.save(commit=True)
44+        new_object = self.form_save_change(request, form)
45         pk_value = new_object._get_pk_val()
46 
47         # Construct the change message. TODO: Temporarily commented-out,