Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added back save_model that was removed from [8273] to allow for both …

…pre- and post- operations around save in ModelAdmin.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@8307 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 8ad96b4d29da98d47df010d864bbab7e8f83a045 1 parent 58cd490
Brian Rosner brosner authored
Showing with 23 additions and 26 deletions.
  1. +12 −12 django/contrib/admin/options.py
  2. +11 −14 docs/admin.txt
24 django/contrib/admin/options.py
View
@@ -437,12 +437,18 @@ def save_form(self, request, form, change):
the object is being changed, and False if it's being added.
"""
return form.save(commit=False)
+
+ def save_model(self, request, obj, form, change):
+ """
+ Given a model instance save it to the database.
+ """
+ obj.save()
def save_formset(self, request, form, formset, change):
"""
- Given an inline formset return unsaved instances.
+ Given an inline formset save it to the database.
"""
- return formset.save(commit=False)
+ formset.save()
def render_change_form(self, request, context, add=False, change=False, form_url='', obj=None):
opts = self.model._meta
@@ -562,13 +568,10 @@ def add_view(self, request, form_url='', extra_context=None):
save_as_new=request.POST.has_key("_saveasnew"))
formsets.append(formset)
if all_valid(formsets) and form_validated:
- new_object.save()
+ self.save_model(request, new_object, form, change=False)
form.save_m2m()
for formset in formsets:
- instances = self.save_formset(request, form, formset, change=False)
- for instance in instances:
- instance.save()
- formset.save_m2m()
+ self.save_formset(request, form, formset, change=False)
self.log_addition(request, new_object)
return self.response_add(request, new_object)
@@ -642,13 +645,10 @@ def change_view(self, request, object_id, extra_context=None):
formsets.append(formset)
if all_valid(formsets) and form_validated:
- new_object.save()
+ self.save_model(request, new_object, form, change=True)
form.save_m2m()
for formset in formsets:
- instances = self.save_formset(request, form, formset, change=True)
- for instance in instances:
- instance.save()
- formset.save_m2m()
+ self.save_formset(request, form, formset, change=True)
change_message = self.construct_change_message(request, form, formsets)
self.log_change(request, new_object, change_message)
25 docs/admin.txt
View
@@ -524,21 +524,19 @@ with an operator:
``ModelAdmin`` methods
----------------------
-``save_form(self, request, form, change)``
+``save_model(self, request, obj, form, change)``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-The ``save_form`` method is given the ``HttpRequest``, a ``ModelForm``
-instance and a boolean value based on whether it is adding or changing the
-object.
+The ``save_model`` method is given the ``HttpRequest``, a model instance,
+a ``ModelForm`` instance and a boolean value based on whether it is adding or
+changing the object. Here you can do any pre- or post-save operations.
-This method should return an unsaved instance. For example to attach
-``request.user`` to the object prior to saving::
+For example to attach ``request.user`` to the object prior to saving::
class ArticleAdmin(admin.ModelAdmin):
- def save_form(self, request, form, change):
- instance = form.save(commit=False)
- instance.user = request.user
- return instance
+ def save_model(self, request, obj, form, change):
+ obj.user = request.user
+ obj.save()
``save_formset(self, request, form, formset, change)``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -547,9 +545,7 @@ The ``save_formset`` method is given the ``HttpRequest``, the parent
``ModelForm`` instance and a boolean value baesed on whether it is adding or
changing the parent object.
-This method should return unsaved instances. These instances will later be
-saved to the database. By default the formset will only return instances that
-have changed. For example to attach ``request.user`` to each changed formset
+For example to attach ``request.user`` to each changed formset
model instance::
class ArticleAdmin(admin.ModelAdmin):
@@ -557,7 +553,8 @@ model instance::
instances = formset.save(commit=False)
for instance in instances:
instance.user = request.user
- return instances
+ instance.save()
+ formset.save_m2m()
``ModelAdmin`` media definitions
--------------------------------
Please sign in to comment.
Something went wrong with that request. Please try again.