Changes between Initial Version and Version 1 of Ticket #8005


Ignore:
Timestamp:
Jul 28, 2008, 8:52:51 AM (16 years ago)
Author:
simon
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #8005 – Description

    initial v1  
    11A very common requirement for customising the admin is to automatically update a created_by or updated_by field with the user object that made changes to an object. This is currently way harder than it should be: http://www.djangosnippets.org/snippets/903/
    22
    3 I propose adding an extra subclassable method that makes it trivial to intercept the model as it is being created and perform additional work on it. Here's an example (incomplete) patch illustrating the idea:
     3I propose adding an extra subclassable method that makes it trivial to intercept the model as it is being created and perform additional work on it. Here's an example (incomplete) patch illustrating the idea: http://code.djangoproject.com/attachment/ticket/8005/8005.1.diff
    44
    5 {{{
    6 Index: django/contrib/admin/options.py
    7 ===================================================================
    8 --- django/contrib/admin/options.py     (revision 8129)
    9 +++ django/contrib/admin/options.py     (working copy)
    10 @@ -324,7 +324,19 @@
    11      def get_formsets(self, request, obj=None):
    12          for inline in self.inline_instances:
    13              yield inline.get_formset(request, obj)
    14 +   
    15 +    def save_model_add(self, request, form, formsets):
    16 +        new_object = form.save(commit=True)
    17 +         
    18 +        if formsets:
    19 +            for formset in formsets:
    20 +                # HACK: it seems like the parent obejct should be passed into
    21 +                # a method of something, not just set as an attribute
    22 +                formset.instance = new_object
    23 +                formset.save()
    24 +       
    25 +        return new_object
    26 +   
    27      def save_add(self, request, form, formsets, post_url_continue):
    28          """
    29          Saves the object in the "add" stage and returns an HttpResponseRedirect.
    30 @@ -333,15 +345,9 @@
    31          """
    32          from django.contrib.admin.models import LogEntry, ADDITION
    33          opts = self.model._meta
    34 -        new_object = form.save(commit=True)
    35 -
    36 -        if formsets:
    37 -            for formset in formsets:
    38 -                # HACK: it seems like the parent obejct should be passed into
    39 -                # a method of something, not just set as an attribute
    40 -                formset.instance = new_object
    41 -                formset.save()
    42 -
    43 +       
    44 +        new_object = self.save_model_add(request, form, formsets)
    45 +       
    46          pk_value = new_object._get_pk_val()
    47          LogEntry.objects.log_action(request.user.id, ContentType.objects.get_for_model(self.model).id,
    48              pk_value, force_unicode(new_object), ADDITION)
    49          msg = _('The %(name)s "%(obj)s" was added successfully.') % {'name': opts.verbose_name, 'obj': new_object}
    50 @@ -371,7 +377,16 @@
    51                  post_url = '../../../'
    52              return HttpResponseRedirect(post_url)
    53      save_add = transaction.commit_on_success(save_add)
    54 -
    55 +   
    56 +    def save_model_change(self, request, form, formsets):
    57 +        new_object = form.save(commit=True)
    58 +       
    59 +        if formsets:
    60 +            for formset in formsets:
    61 +                formset.save()
    62 +       
    63 +        return new_object
    64 +   
    65      def save_change(self, request, form, formsets=None):
    66          """
    67          Saves the object in the "change" stage and returns an HttpResponseRedirect.
    68 @@ -382,13 +397,11 @@
    69          """
    70          from django.contrib.admin.models import LogEntry, CHANGE
    71          opts = self.model._meta
    72 -        new_object = form.save(commit=True)
    73 +       
    74 +        new_object = self.save_model_change(request, form, formsets)
    75 +       
    76          pk_value = new_object._get_pk_val()
    77 -
    78 -        if formsets:
    79 -            for formset in formsets:
    80 -                formset.save()
    81 -
    82 +       
    83          # Construct the change message.
    84          change_message = []
    85          if form.changed_data:
    86 }}}
    87 The above would allow ModelAdmin subclasses to make additional changes to the model by calling form.save(commit=False) instead. For the admin user described above, the subclass would look like this:
     5This would allow ModelAdmin subclasses to make additional changes to the model by calling form.save(commit=False) instead. For the admin user described above, the subclass would look like this:
    886{{{
    897class ArticleAdmin(admin.ModelAdmin):
Back to Top