Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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 authored August 11, 2008
24  django/contrib/admin/options.py
@@ -437,12 +437,18 @@ def save_form(self, request, form, change):
437 437
         the object is being changed, and False if it's being added.
438 438
         """
439 439
         return form.save(commit=False)
  440
+    
  441
+    def save_model(self, request, obj, form, change):
  442
+        """
  443
+        Given a model instance save it to the database.
  444
+        """
  445
+        obj.save()
440 446
 
441 447
     def save_formset(self, request, form, formset, change):
442 448
         """
443  
-        Given an inline formset return unsaved instances.
  449
+        Given an inline formset save it to the database.
444 450
         """
445  
-        return formset.save(commit=False)
  451
+        formset.save()
446 452
 
447 453
     def render_change_form(self, request, context, add=False, change=False, form_url='', obj=None):
448 454
         opts = self.model._meta
@@ -562,13 +568,10 @@ def add_view(self, request, form_url='', extra_context=None):
562 568
                                   save_as_new=request.POST.has_key("_saveasnew"))
563 569
                 formsets.append(formset)
564 570
             if all_valid(formsets) and form_validated:
565  
-                new_object.save()
  571
+                self.save_model(request, new_object, form, change=False)
566 572
                 form.save_m2m()
567 573
                 for formset in formsets:
568  
-                    instances = self.save_formset(request, form, formset, change=False)
569  
-                    for instance in instances:
570  
-                        instance.save()
571  
-                    formset.save_m2m()
  574
+                    self.save_formset(request, form, formset, change=False)
572 575
                 
573 576
                 self.log_addition(request, new_object)
574 577
                 return self.response_add(request, new_object)
@@ -642,13 +645,10 @@ def change_view(self, request, object_id, extra_context=None):
642 645
                 formsets.append(formset)
643 646
 
644 647
             if all_valid(formsets) and form_validated:
645  
-                new_object.save()
  648
+                self.save_model(request, new_object, form, change=True)
646 649
                 form.save_m2m()
647 650
                 for formset in formsets:
648  
-                    instances = self.save_formset(request, form, formset, change=True)
649  
-                    for instance in instances:
650  
-                        instance.save()
651  
-                    formset.save_m2m()
  651
+                    self.save_formset(request, form, formset, change=True)
652 652
                 
653 653
                 change_message = self.construct_change_message(request, form, formsets)
654 654
                 self.log_change(request, new_object, change_message)
25  docs/admin.txt
@@ -524,21 +524,19 @@ with an operator:
524 524
 ``ModelAdmin`` methods
525 525
 ----------------------
526 526
 
527  
-``save_form(self, request, form, change)``
  527
+``save_model(self, request, obj, form, change)``
528 528
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
529 529
 
530  
-The ``save_form`` method is given the ``HttpRequest``, a ``ModelForm``
531  
-instance and a boolean value based on whether it is adding or changing the
532  
-object.
  530
+The ``save_model`` method is given the ``HttpRequest``, a model instance,
  531
+a ``ModelForm`` instance and a boolean value based on whether it is adding or
  532
+changing the object. Here you can do any pre- or post-save operations.
533 533
 
534  
-This method should return an unsaved instance. For example to attach
535  
-``request.user`` to the object prior to saving::
  534
+For example to attach ``request.user`` to the object prior to saving::
536 535
 
537 536
     class ArticleAdmin(admin.ModelAdmin):
538  
-        def save_form(self, request, form, change):
539  
-            instance = form.save(commit=False)
540  
-            instance.user = request.user
541  
-            return instance
  537
+        def save_model(self, request, obj, form, change):
  538
+            obj.user = request.user
  539
+            obj.save()
542 540
 
543 541
 ``save_formset(self, request, form, formset, change)``
544 542
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -547,9 +545,7 @@ The ``save_formset`` method is given the ``HttpRequest``, the parent
547 545
 ``ModelForm`` instance and a boolean value baesed on whether it is adding or
548 546
 changing the parent object.
549 547
 
550  
-This method should return unsaved instances. These instances will later be
551  
-saved to the database. By default the formset will only return instances that
552  
-have changed. For example to attach ``request.user`` to each changed formset
  548
+For example to attach ``request.user`` to each changed formset
553 549
 model instance::
554 550
 
555 551
     class ArticleAdmin(admin.ModelAdmin):
@@ -557,7 +553,8 @@ model instance::
557 553
             instances = formset.save(commit=False)
558 554
             for instance in instances:
559 555
                 instance.user = request.user
560  
-            return instances
  556
+                instance.save()
  557
+            formset.save_m2m()
561 558
 
562 559
 ``ModelAdmin`` media definitions
563 560
 --------------------------------

0 notes on commit 8ad96b4

Please sign in to comment.
Something went wrong with that request. Please try again.