Fixed #21013 -- Ensure that ModelAdmin.get_queryset is considered for the admin history view.

This commit is contained in:
Juan Catalano 2013-09-06 20:28:48 +00:00 committed by Julien Phalip
parent e192739b3e
commit 04415bf81b
5 changed files with 42 additions and 3 deletions

View File

@ -144,6 +144,7 @@ answer newbie questions, and generally made Django that much better:
Ricardo Javier Cárdenes Medina <ricardo.cardenes@gmail.com> Ricardo Javier Cárdenes Medina <ricardo.cardenes@gmail.com>
Jeremy Carbaugh <jcarbaugh@gmail.com> Jeremy Carbaugh <jcarbaugh@gmail.com>
Graham Carlyle <graham.carlyle@maplecroft.net> Graham Carlyle <graham.carlyle@maplecroft.net>
Juan Catalano <catalanojuan@gmail.com>
Antonio Cavedoni <http://cavedoni.com/> Antonio Cavedoni <http://cavedoni.com/>
cedric@terramater.net cedric@terramater.net
Chris Chamberlin <dja@cdc.msbx.net> Chris Chamberlin <dja@cdc.msbx.net>

View File

@ -1537,7 +1537,7 @@ class ModelAdmin(BaseModelAdmin):
from django.contrib.admin.models import LogEntry from django.contrib.admin.models import LogEntry
# First check if the user can see this history. # First check if the user can see this history.
model = self.model model = self.model
obj = get_object_or_404(model, pk=unquote(object_id)) obj = get_object_or_404(self.get_queryset(request), pk=unquote(object_id))
if not self.has_change_permission(request, obj): if not self.has_change_permission(request, obj):
raise PermissionDenied raise PermissionDenied

View File

@ -30,7 +30,7 @@ from .models import (Article, Chapter, Account, Media, Child, Parent, Picture,
AdminOrderedField, AdminOrderedModelMethod, AdminOrderedAdminMethod, AdminOrderedField, AdminOrderedModelMethod, AdminOrderedAdminMethod,
AdminOrderedCallable, Report, Color2, UnorderedObject, MainPrepopulated, AdminOrderedCallable, Report, Color2, UnorderedObject, MainPrepopulated,
RelatedPrepopulated, UndeletableObject, UnchangeableObject, UserMessenger, Simple, Choice, RelatedPrepopulated, UndeletableObject, UnchangeableObject, UserMessenger, Simple, Choice,
ShortMessage, Telegram) ShortMessage, Telegram, FilteredManager)
def callable_year(dt_value): def callable_year(dt_value):
@ -674,6 +674,12 @@ def callable_on_unknown(obj):
class AttributeErrorRaisingAdmin(admin.ModelAdmin): class AttributeErrorRaisingAdmin(admin.ModelAdmin):
list_display = [callable_on_unknown, ] list_display = [callable_on_unknown, ]
class CustomManagerAdmin(admin.ModelAdmin):
def get_queryset(self, request):
return FilteredManager.objects
class MessageTestingAdmin(admin.ModelAdmin): class MessageTestingAdmin(admin.ModelAdmin):
actions = ["message_debug", "message_info", "message_success", actions = ["message_debug", "message_info", "message_success",
"message_warning", "message_error", "message_extra_tags"] "message_warning", "message_error", "message_extra_tags"]
@ -773,6 +779,7 @@ site.register(Question)
site.register(Answer) site.register(Answer)
site.register(PrePopulatedPost, PrePopulatedPostAdmin) site.register(PrePopulatedPost, PrePopulatedPostAdmin)
site.register(ComplexSortedPerson, ComplexSortedPersonAdmin) site.register(ComplexSortedPerson, ComplexSortedPersonAdmin)
site.register(FilteredManager, CustomManagerAdmin)
site.register(PluggableSearchPerson, PluggableSearchPersonAdmin) site.register(PluggableSearchPerson, PluggableSearchPersonAdmin)
site.register(PrePopulatedPostLargeSlug, PrePopulatedPostLargeSlugAdmin) site.register(PrePopulatedPostLargeSlug, PrePopulatedPostLargeSlugAdmin)
site.register(AdminOrderedField, AdminOrderedFieldAdmin) site.register(AdminOrderedField, AdminOrderedFieldAdmin)

View File

@ -693,3 +693,14 @@ class Simple(models.Model):
class Choice(models.Model): class Choice(models.Model):
choice = models.IntegerField(blank=True, null=True, choice = models.IntegerField(blank=True, null=True,
choices=((1, 'Yes'), (0, 'No'), (None, 'No opinion'))) choices=((1, 'Yes'), (0, 'No'), (None, 'No opinion')))
class _Manager(models.Manager):
def get_queryset(self):
return super(_Manager, self).get_queryset().filter(pk__gt=1)
class FilteredManager(models.Model):
def __str__(self):
return "PK=%d" % self.pk
pk_gt_1 = _Manager()
objects = models.Manager()

View File

@ -48,7 +48,7 @@ from .models import (Article, BarAccount, CustomArticle, EmptyModel, FooAccount,
AdminOrderedModelMethod, AdminOrderedAdminMethod, AdminOrderedCallable, AdminOrderedModelMethod, AdminOrderedAdminMethod, AdminOrderedCallable,
Report, MainPrepopulated, RelatedPrepopulated, UnorderedObject, Report, MainPrepopulated, RelatedPrepopulated, UnorderedObject,
Simple, UndeletableObject, UnchangeableObject, Choice, ShortMessage, Simple, UndeletableObject, UnchangeableObject, Choice, ShortMessage,
Telegram, Pizza, Topping) Telegram, Pizza, Topping, FilteredManager)
from .admin import site, site2 from .admin import site, site2
@ -2656,6 +2656,12 @@ class AdminCustomQuerysetTest(TestCase):
def setUp(self): def setUp(self):
self.client.login(username='super', password='secret') self.client.login(username='super', password='secret')
self.pks = [EmptyModel.objects.create().id for i in range(3)] self.pks = [EmptyModel.objects.create().id for i in range(3)]
self.super_login = {
REDIRECT_FIELD_NAME: '/test_admin/admin/',
LOGIN_FORM_KEY: 1,
'username': 'super',
'password': 'secret',
}
def test_changelist_view(self): def test_changelist_view(self):
response = self.client.get('/test_admin/admin/admin_views/emptymodel/') response = self.client.get('/test_admin/admin/admin_views/emptymodel/')
@ -2873,6 +2879,20 @@ class AdminCustomQuerysetTest(TestCase):
html=True html=True
) )
def test_history_view_custom_qs(self):
"""
Ensure that custom querysets are considered for the admin history view.
Refs #21013.
"""
self.client.post('/test_admin/admin/', self.super_login)
FilteredManager.objects.create(pk=1)
FilteredManager.objects.create(pk=2)
response = self.client.get('/test_admin/admin/admin_views/filteredmanager/')
self.assertContains(response, "PK=1")
self.assertContains(response, "PK=2")
self.assertEqual(self.client.get('/test_admin/admin/admin_views/filteredmanager/1/history/').status_code, 200)
self.assertEqual(self.client.get('/test_admin/admin/admin_views/filteredmanager/2/history/').status_code, 200)
@override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',)) @override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))
class AdminInlineFileUploadTest(TestCase): class AdminInlineFileUploadTest(TestCase):