Django

Code

Ticket #7510 (closed: fixed)

Opened 2 years ago

Last modified 10 months ago

ModelAdmin should be able to use a non-default manager

Reported by: tom Assigned to: Alex
Milestone: 1.2 Component: django.contrib.admin
Version: SVN Keywords:
Cc: gonz@webstudio.com.uy, torsten.rehn@dystopian.info Triage Stage: Accepted
Has patch: 1 Needs documentation: 1
Needs tests: 1 Patch needs improvement: 1

Description

At the moment the change_view and delete_view methods of ModelAdmin? in django/contrib/admin/options.py both use the default manager of the model when retrieving an instance for change or deletion. However, there is a "queryset" method on the same class that is used to get the list of models to display in the changelist. This means that to remove certain instances from the admin one has to both update the queryset method to filter them out and then change the 3 has_*_permission methods. This doesn't seem very DRY to me.

I propose changing the change_view, delete_view and history_view methods so that they simply use the queryset returned by the queryset method instead of the default managers. This means that an filters put on the queryset by the queryset method are honored and there is thus only one place to change when you want to remove a subset of objects from consideration.

This seems like a useful change to make to me and I can't see any problems it would create but maybe I'm missing something...

Attachments

use_queryset_in_delete_view_and_change_view_for_ModelAdmin.diff (1.6 kB) - added by tom on 06/20/08 04:47:57.
modeladmin_override_manager.diff (1.8 kB) - added by jfw on 06/22/08 21:46:09.
admin-qs.diff (4.0 kB) - added by Alex on 07/19/08 16:00:29.
Patch with tests
options.diff (1.9 kB) - added by jjackson on 09/05/08 16:53:28.
Patch as of rev 8861 or Django 1.0.
admin-qs.2.diff (4.1 kB) - added by Alex on 03/31/09 15:12:09.

Change History

06/20/08 04:47:57 changed by tom

  • attachment use_queryset_in_delete_view_and_change_view_for_ModelAdmin.diff added.

06/22/08 21:46:09 changed by jfw

  • attachment modeladmin_override_manager.diff added.

06/22/08 21:46:45 changed by jfw

  • needs_better_patch changed.
  • needs_tests changed.
  • needs_docs changed.

I also found a need to have change_view use a different queryset than the _default_manager's one. Before seeing the above patch, I had created my own. I think this one (attached) adds a little more flexibility, as it lets you override the manager used in all of the admin site. You can still override the queryset function, but it might be less necessary to do so.

07/19/08 16:00:29 changed by Alex

  • attachment admin-qs.diff added.

Patch with tests

07/19/08 16:01:21 changed by Alex

  • version changed from newforms-admin to SVN.

08/08/08 16:24:10 changed by ericholscher

  • stage changed from Unreviewed to Design decision needed.
  • milestone set to post-1.0.

08/26/08 01:05:10 changed by gonz

  • cc set to gonz@webstudio.com.uy.

09/05/08 16:51:22 changed by jjackson

Here's another patch to handle this problem. I used this to fix the problem in Practical Django Projects.

After the patch has been applied, you can then use in your model an order such as

class Entry(models.Model):
    live = LiveEntryManager()
    objects = models.Manager()

and then in your admin.py use

class EntryAdmin(admin.ModelAdmin):
    prepopulated_fields = {'slug': ('title',)}
    model_admin_manager = Entry.objects

to set the manager to be used by the admin application for the Entry class. In this case, we wanted to see only the live entries on the website, but to see all the entries in admin app. Otherwise, after setting a Entry to "draft", you would never be able to edit it again.

09/05/08 16:53:28 changed by jjackson

  • attachment options.diff added.

Patch as of rev 8861 or Django 1.0.

09/19/08 17:41:01 changed by scel

  • cc changed from gonz@webstudio.com.uy to gonz@webstudio.com.uy, torsten.rehn@dystopian.info.
  • summary changed from Patch to have change_view and delete_view methods of ModelAdmin use queryset the queryset method to ModelAdmin should be able to use a non-default manager.

I changed the summary as I think the ability to use a non-default manager is more important and implies the original request to be honored.

I also want to point out that the documentation on this topic is incorrect (at least as I read it):

Django interprets this first
``Manager`` defined in a class as the "default" ``Manager``, and
several parts of Django (though not the admin application) will use
that ``Manager`` exclusively for that model.

from docs/topics/db/managers.txt:170-173 or online docs

As I interpret this, the admin is supposed to always use a "bare" Manager(), not the default manager, as it is currently doing.

Plus, this makes it impossible to use a custom manager in related lookups, but not the admin (use_for_related_fields can only be used in the default manager, I don't really know why there is no Model.Meta.related_manager option).

10/02/08 18:14:53 changed by Alex

  • owner changed from nobody to Alex.

11/10/08 17:42:37 changed by SmileyChris

  • needs_docs set to 1.
  • stage changed from Design decision needed to Accepted.
  • milestone deleted.

scel is correct that the current documentation is out of date.

brosner agrees that this is issue valid.

A small suggestion for the patch: just set model_manager = None as a ModelAdmin class attribute, then you don't have to worry about hasattr and it's a bit more of an obvious API attribute (model_admin_manager seems overly verbose - of course it's for admin ;))

12/03/08 15:32:05 changed by niels

  • cc changed from gonz@webstudio.com.uy, torsten.rehn@dystopian.info to gonz@webstudio.com.uy, torsten.rehn@dystopian.info, niels.busch@gmail.com.

02/17/09 04:53:54 changed by niels

  • cc changed from gonz@webstudio.com.uy, torsten.rehn@dystopian.info, niels.busch@gmail.com to gonz@webstudio.com.uy, torsten.rehn@dystopian.info.

03/27/09 02:08:34 changed by mrts

  • needs_better_patch set to 1.
  • needs_tests set to 1.
  • milestone set to 1.2.

Looks useful, tentatively pushing to 1.2.

03/31/09 15:12:09 changed by Alex

  • attachment admin-qs.2.diff added.

03/31/09 22:11:59 changed by jacob

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

(In [10314]) Fixed #7510: the ModelAdmin? now uses self.queryset instead of the default manager. Thanks, Alex Gaynor.


Add/Change #7510 (ModelAdmin should be able to use a non-default manager)




Change Properties
Action