Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added ModelAdmin.save_model() and ModelAdmin.save_formset() methods t…

…o allow for easier modification of objects/inlines at admin-save time. Refs #6002.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@8266 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit f53e4d80b488d0e673182bb7565c97f081833f68 1 parent 6ba6489
Jacob Kaplan-Moss authored August 09, 2008

Showing 1 changed file with 18 additions and 7 deletions. Show diff stats Hide diff stats

  1. 25  django/contrib/admin/options.py
25  django/contrib/admin/options.py
@@ -452,6 +452,19 @@ def message_user(self, request, message):
452 452
         """
453 453
         request.user.message_set.create(message=message)
454 454
 
  455
+    def save_model(self, request, form, change):
  456
+        """
  457
+        Save and return a model given a ModelForm. ``change`` is True if the
  458
+        object is being changed, and False if it's being added.
  459
+        """
  460
+        return form.save(commit=True)
  461
+
  462
+    def save_formset(self, request, form, formset, change):
  463
+        """
  464
+        Save an inline formset attached to the object.
  465
+        """
  466
+        formset.save()
  467
+
455 468
     def save_add(self, request, form, formsets, post_url_continue):
456 469
         """
457 470
         Saves the object in the "add" stage and returns an HttpResponseRedirect.
@@ -459,14 +472,12 @@ def save_add(self, request, form, formsets, post_url_continue):
459 472
         `form` is a bound Form instance that's verified to be valid.
460 473
         """
461 474
         opts = self.model._meta
462  
-        new_object = form.save(commit=True)
463  
-
  475
+        
  476
+        new_object = self.save_model(request, form, change=False)
464 477
         if formsets:
465 478
             for formset in formsets:
466  
-                # HACK: it seems like the parent obejct should be passed into
467  
-                # a method of something, not just set as an attribute
468 479
                 formset.instance = new_object
469  
-                formset.save()
  480
+                self.save_formset(request, form, formset, change=False)
470 481
 
471 482
         pk_value = new_object._get_pk_val()
472 483
         self.log_addition(request, new_object)
@@ -509,12 +520,12 @@ def save_change(self, request, form, formsets=None):
509 520
         `formsets` is a sequence of InlineFormSet instances that are verified to be valid.
510 521
         """
511 522
         opts = self.model._meta
512  
-        new_object = form.save(commit=True)
  523
+        new_object = self.save_model(request, form, change=True)
513 524
         pk_value = new_object._get_pk_val()
514 525
 
515 526
         if formsets:
516 527
             for formset in formsets:
517  
-                formset.save()
  528
+                self.save_formset(request, form, formset, change=True)
518 529
         
519 530
         change_message = self.construct_change_message(request, form, formsets)
520 531
         self.log_change(request, new_object, change_message)        

0 notes on commit f53e4d8

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