Django

Code

Ticket #6002 (closed: fixed)

Opened 2 years ago

Last modified 2 years ago

Better saving in newforms-admin ModelAdmin

Reported by: Petr Marhoun <petr.marhoun@gmail.com> Assigned to: jacob
Milestone: 1.0 beta Component: django.contrib.admin
Version: SVN Keywords:
Cc: Triage Stage: Accepted
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 1

Description

There are two very long methods in django.contrib.admin.options.ModelAdmin - save_add and save_change. They do three different things - they save objects, they log what happened and they redirect. So I propose to divide them.

Method add_view would call save_add for saving, log_add for logging and redirect_after_save for redirection. Method change_view would call save_change for saving, log_change for logging and redirect_after_save for redirection.

Methods save_add and save_change would be useful for complex saving which depends on forms and inline formsets together. (It solves #4507.)

Methods log_add and log_change could be used for example for something as AuditModelAdmin or NoLogAdmin.

Method redirect_after_save would be something as render_change_form - technical code common for adding and changing, which is not interesting very much.

I feel that now redirection is quite broken, it does not check permissions correctly. My patch fixes it too.

Attachments

00-admin-better-saving.diff (11.0 kB) - added by Petr Marhoun <petr.marhoun@gmail.com> on 11/20/07 23:03:16.
00-admin-better-saving.2.diff (11.1 kB) - added by Petr Marhoun <petr.marhoun@gmail.com> on 11/30/07 09:28:13.
new version - after autoescape merge
00-admin-better-saving.3.diff (11.1 kB) - added by Petr Marhoun <petr.marhoun@gmail.com> on 11/30/07 09:29:04.
new version - after autoescape merge
00-admin-better-saving.4.diff (11.1 kB) - added by Petr Marhoun <petr.marhoun@gmail.com> on 12/08/07 14:26:58.
00-admin-better-saving.5.diff (12.0 kB) - added by shnur on 07/10/08 03:05:04.
Patch cleanly applies to revision 7875

Change History

11/20/07 23:03:16 changed by Petr Marhoun <petr.marhoun@gmail.com>

  • attachment 00-admin-better-saving.diff added.

11/30/07 09:28:13 changed by Petr Marhoun <petr.marhoun@gmail.com>

  • attachment 00-admin-better-saving.2.diff added.

new version - after autoescape merge

11/30/07 09:29:04 changed by Petr Marhoun <petr.marhoun@gmail.com>

  • attachment 00-admin-better-saving.3.diff added.

new version - after autoescape merge

12/07/07 20:19:22 changed by brosner

  • keywords set to nfa-blocker.
  • needs_better_patch changed.
  • stage changed from Unreviewed to Accepted.
  • needs_tests changed.
  • needs_docs changed.

This should be solved with the use of ModelForm once it is integrated into ModelAdmin?.

12/08/07 14:26:58 changed by Petr Marhoun <petr.marhoun@gmail.com>

  • attachment 00-admin-better-saving.4.diff added.

02/21/08 08:20:48 changed by peschler

Just some little addition: Add a post_url parameter to the proposed redirect_after_save method:

def redirect_after_save(self, request, new_obj, add=False, change=False, post_url="../")

Then use this to redirect when succesfully saved.

Rationale: Consider a Menu model which contains MenuEntry? models. In the admin I want to edit the Menus, MenuEntries? should only be accessible through the admin view of the Menu. If a MenuEntry? was edited I want to redirect to the Menu it is contained in, rather than to the MenuEntry? overview. To achieve this I need to overwrite save_change (in newforms-admin trunk) or redirect_after_save (in the patch version) and duplicate the whole code of these functions. With the additional post_url parameter a simple wrapper would suffice.

06/16/08 10:47:27 changed by garcia_marc

  • milestone set to 1.0 alpha.

07/10/08 03:05:04 changed by shnur

  • attachment 00-admin-better-saving.5.diff added.

Patch cleanly applies to revision 7875

07/10/08 04:21:23 changed by shnur

  • owner changed from nobody to shnur.
  • status changed from new to assigned.

07/15/08 23:15:16 changed by brosner

  • keywords deleted.
  • milestone changed from 1.0 alpha to 1.0 beta.

This actually really isn't critical for a merge. I am going to drop the nfa-blocker and bump to 1.0 beta. I will look at it later.

08/09/08 11:00:34 changed by jacob

#6406 and #8005 were marked as dups; they're not quite the same problem, but the problems there should be solved by a general refactoring of saving. Both have some interesting code, though.

08/09/08 11:12:31 changed by jacob

  • owner changed from shnur to jacob.
  • needs_better_patch set to 1.
  • version changed from newforms-admin to SVN.
  • status changed from assigned to new.

08/09/08 11:12:38 changed by jacob

  • status changed from new to assigned.

08/09/08 11:45:20 changed by jacob

(In [8265]) Broke the admin's use of LogEntry? and user messages out into callbacks on ModelAdmin?. Refs #6002.

08/09/08 12:12:48 changed by jacob

(In [8266]) Added ModelAdmin?.save_model() and ModelAdmin?.save_formset() methods to allow for easier modification of objects/inlines at admin-save time. Refs #6002.

08/09/08 12:13:35 changed by jacob

  • status changed from assigned to closed.
  • resolution set to fixed.

[8266] should have said "fixed" instead of "refs"; this is done now.


Add/Change #6002 (Better saving in newforms-admin ModelAdmin)




Change Properties
Action