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
This commit is contained in:
Brian Rosner 2008-08-11 17:20:47 +00:00
parent 58cd4902a7
commit 8ad96b4d29
2 changed files with 23 additions and 26 deletions

View File

@ -437,12 +437,18 @@ class ModelAdmin(BaseModelAdmin):
the object is being changed, and False if it's being added. the object is being changed, and False if it's being added.
""" """
return form.save(commit=False) 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): 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): def render_change_form(self, request, context, add=False, change=False, form_url='', obj=None):
opts = self.model._meta opts = self.model._meta
@ -562,13 +568,10 @@ class ModelAdmin(BaseModelAdmin):
save_as_new=request.POST.has_key("_saveasnew")) save_as_new=request.POST.has_key("_saveasnew"))
formsets.append(formset) formsets.append(formset)
if all_valid(formsets) and form_validated: if all_valid(formsets) and form_validated:
new_object.save() self.save_model(request, new_object, form, change=False)
form.save_m2m() form.save_m2m()
for formset in formsets: for formset in formsets:
instances = self.save_formset(request, form, formset, change=False) self.save_formset(request, form, formset, change=False)
for instance in instances:
instance.save()
formset.save_m2m()
self.log_addition(request, new_object) self.log_addition(request, new_object)
return self.response_add(request, new_object) return self.response_add(request, new_object)
@ -642,13 +645,10 @@ class ModelAdmin(BaseModelAdmin):
formsets.append(formset) formsets.append(formset)
if all_valid(formsets) and form_validated: if all_valid(formsets) and form_validated:
new_object.save() self.save_model(request, new_object, form, change=True)
form.save_m2m() form.save_m2m()
for formset in formsets: for formset in formsets:
instances = self.save_formset(request, form, formset, change=True) self.save_formset(request, form, formset, change=True)
for instance in instances:
instance.save()
formset.save_m2m()
change_message = self.construct_change_message(request, form, formsets) change_message = self.construct_change_message(request, form, formsets)
self.log_change(request, new_object, change_message) self.log_change(request, new_object, change_message)

View File

@ -524,21 +524,19 @@ with an operator:
``ModelAdmin`` methods ``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`` The ``save_model`` method is given the ``HttpRequest``, a model instance,
instance and a boolean value based on whether it is adding or changing the a ``ModelForm`` instance and a boolean value based on whether it is adding or
object. changing the object. Here you can do any pre- or post-save operations.
This method should return an unsaved instance. For example to attach For example to attach ``request.user`` to the object prior to saving::
``request.user`` to the object prior to saving::
class ArticleAdmin(admin.ModelAdmin): class ArticleAdmin(admin.ModelAdmin):
def save_form(self, request, form, change): def save_model(self, request, obj, form, change):
instance = form.save(commit=False) obj.user = request.user
instance.user = request.user obj.save()
return instance
``save_formset(self, request, form, formset, change)`` ``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 ``ModelForm`` instance and a boolean value baesed on whether it is adding or
changing the parent object. changing the parent object.
This method should return unsaved instances. These instances will later be For example to attach ``request.user`` to each changed formset
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
model instance:: model instance::
class ArticleAdmin(admin.ModelAdmin): class ArticleAdmin(admin.ModelAdmin):
@ -557,7 +553,8 @@ model instance::
instances = formset.save(commit=False) instances = formset.save(commit=False)
for instance in instances: for instance in instances:
instance.user = request.user instance.user = request.user
return instances instance.save()
formset.save_m2m()
``ModelAdmin`` media definitions ``ModelAdmin`` media definitions
-------------------------------- --------------------------------