Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #21013 -- Ensure that ModelAdmin.get_queryset is considered for…

… the admin history view.
  • Loading branch information...
commit 04415bf81b2440a08fc224e6c481bcb5840bbcbc 1 parent e192739
@catalanojuan catalanojuan authored jphalip committed
View
1  AUTHORS
@@ -144,6 +144,7 @@ answer newbie questions, and generally made Django that much better:
Ricardo Javier Cárdenes Medina <ricardo.cardenes@gmail.com>
Jeremy Carbaugh <jcarbaugh@gmail.com>
Graham Carlyle <graham.carlyle@maplecroft.net>
+ Juan Catalano <catalanojuan@gmail.com>
Antonio Cavedoni <http://cavedoni.com/>
cedric@terramater.net
Chris Chamberlin <dja@cdc.msbx.net>
View
2  django/contrib/admin/options.py
@@ -1537,7 +1537,7 @@ def history_view(self, request, object_id, extra_context=None):
from django.contrib.admin.models import LogEntry
# First check if the user can see this history.
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):
raise PermissionDenied
View
9 tests/admin_views/admin.py
@@ -30,7 +30,7 @@
AdminOrderedField, AdminOrderedModelMethod, AdminOrderedAdminMethod,
AdminOrderedCallable, Report, Color2, UnorderedObject, MainPrepopulated,
RelatedPrepopulated, UndeletableObject, UnchangeableObject, UserMessenger, Simple, Choice,
- ShortMessage, Telegram)
+ ShortMessage, Telegram, FilteredManager)
def callable_year(dt_value):
@@ -674,6 +674,12 @@ def callable_on_unknown(obj):
class AttributeErrorRaisingAdmin(admin.ModelAdmin):
list_display = [callable_on_unknown, ]
+
+class CustomManagerAdmin(admin.ModelAdmin):
+ def get_queryset(self, request):
+ return FilteredManager.objects
+
+
class MessageTestingAdmin(admin.ModelAdmin):
actions = ["message_debug", "message_info", "message_success",
"message_warning", "message_error", "message_extra_tags"]
@@ -773,6 +779,7 @@ class ChoiceList(admin.ModelAdmin):
site.register(Answer)
site.register(PrePopulatedPost, PrePopulatedPostAdmin)
site.register(ComplexSortedPerson, ComplexSortedPersonAdmin)
+site.register(FilteredManager, CustomManagerAdmin)
site.register(PluggableSearchPerson, PluggableSearchPersonAdmin)
site.register(PrePopulatedPostLargeSlug, PrePopulatedPostLargeSlugAdmin)
site.register(AdminOrderedField, AdminOrderedFieldAdmin)
View
11 tests/admin_views/models.py
@@ -693,3 +693,14 @@ class Simple(models.Model):
class Choice(models.Model):
choice = models.IntegerField(blank=True, null=True,
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
22 tests/admin_views/tests.py
@@ -48,7 +48,7 @@
AdminOrderedModelMethod, AdminOrderedAdminMethod, AdminOrderedCallable,
Report, MainPrepopulated, RelatedPrepopulated, UnorderedObject,
Simple, UndeletableObject, UnchangeableObject, Choice, ShortMessage,
- Telegram, Pizza, Topping)
+ Telegram, Pizza, Topping, FilteredManager)
from .admin import site, site2
@@ -2656,6 +2656,12 @@ class AdminCustomQuerysetTest(TestCase):
def setUp(self):
self.client.login(username='super', password='secret')
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):
response = self.client.get('/test_admin/admin/admin_views/emptymodel/')
@@ -2873,6 +2879,20 @@ def test_edit_model_modeladmin_only_qs(self):
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',))
class AdminInlineFileUploadTest(TestCase):
Please sign in to comment.
Something went wrong with that request. Please try again.