mirror of https://github.com/django/django.git
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:
parent
58cd4902a7
commit
8ad96b4d29
|
@ -437,12 +437,18 @@ class ModelAdmin(BaseModelAdmin):
|
|||
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 @@ class ModelAdmin(BaseModelAdmin):
|
|||
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 @@ class ModelAdmin(BaseModelAdmin):
|
|||
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)
|
||||
|
|
|
@ -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
|
||||
--------------------------------
|
||||
|
|
Loading…
Reference in New Issue