Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

… the admin history view.
  • Loading branch information...
commit 04415bf81b2440a08fc224e6c481bcb5840bbcbc 1 parent e192739
Juan Ignacio Catalano authored September 06, 2013 jphalip committed September 07, 2013
1  AUTHORS
@@ -144,6 +144,7 @@ answer newbie questions, and generally made Django that much better:
144 144
     Ricardo Javier Cárdenes Medina <ricardo.cardenes@gmail.com>
145 145
     Jeremy Carbaugh <jcarbaugh@gmail.com>
146 146
     Graham Carlyle <graham.carlyle@maplecroft.net>
  147
+    Juan Catalano <catalanojuan@gmail.com>
147 148
     Antonio Cavedoni <http://cavedoni.com/>
148 149
     cedric@terramater.net
149 150
     Chris Chamberlin <dja@cdc.msbx.net>
2  django/contrib/admin/options.py
@@ -1537,7 +1537,7 @@ def history_view(self, request, object_id, extra_context=None):
1537 1537
         from django.contrib.admin.models import LogEntry
1538 1538
         # First check if the user can see this history.
1539 1539
         model = self.model
1540  
-        obj = get_object_or_404(model, pk=unquote(object_id))
  1540
+        obj = get_object_or_404(self.get_queryset(request), pk=unquote(object_id))
1541 1541
 
1542 1542
         if not self.has_change_permission(request, obj):
1543 1543
             raise PermissionDenied
9  tests/admin_views/admin.py
@@ -30,7 +30,7 @@
30 30
     AdminOrderedField, AdminOrderedModelMethod, AdminOrderedAdminMethod,
31 31
     AdminOrderedCallable, Report, Color2, UnorderedObject, MainPrepopulated,
32 32
     RelatedPrepopulated, UndeletableObject, UnchangeableObject, UserMessenger, Simple, Choice,
33  
-    ShortMessage, Telegram)
  33
+    ShortMessage, Telegram, FilteredManager)
34 34
 
35 35
 
36 36
 def callable_year(dt_value):
@@ -674,6 +674,12 @@ def callable_on_unknown(obj):
674 674
 class AttributeErrorRaisingAdmin(admin.ModelAdmin):
675 675
     list_display = [callable_on_unknown, ]
676 676
 
  677
+
  678
+class CustomManagerAdmin(admin.ModelAdmin):
  679
+    def get_queryset(self, request):
  680
+        return FilteredManager.objects
  681
+
  682
+
677 683
 class MessageTestingAdmin(admin.ModelAdmin):
678 684
     actions = ["message_debug", "message_info", "message_success",
679 685
                "message_warning", "message_error", "message_extra_tags"]
@@ -773,6 +779,7 @@ class ChoiceList(admin.ModelAdmin):
773 779
 site.register(Answer)
774 780
 site.register(PrePopulatedPost, PrePopulatedPostAdmin)
775 781
 site.register(ComplexSortedPerson, ComplexSortedPersonAdmin)
  782
+site.register(FilteredManager, CustomManagerAdmin)
776 783
 site.register(PluggableSearchPerson, PluggableSearchPersonAdmin)
777 784
 site.register(PrePopulatedPostLargeSlug, PrePopulatedPostLargeSlugAdmin)
778 785
 site.register(AdminOrderedField, AdminOrderedFieldAdmin)
11  tests/admin_views/models.py
@@ -693,3 +693,14 @@ class Simple(models.Model):
693 693
 class Choice(models.Model):
694 694
     choice = models.IntegerField(blank=True, null=True,
695 695
         choices=((1, 'Yes'), (0, 'No'), (None, 'No opinion')))
  696
+
  697
+class _Manager(models.Manager):
  698
+    def get_queryset(self):
  699
+        return super(_Manager, self).get_queryset().filter(pk__gt=1)
  700
+
  701
+class FilteredManager(models.Model):
  702
+    def __str__(self):
  703
+        return "PK=%d" % self.pk
  704
+
  705
+    pk_gt_1 = _Manager()
  706
+    objects = models.Manager()
22  tests/admin_views/tests.py
@@ -48,7 +48,7 @@
48 48
     AdminOrderedModelMethod, AdminOrderedAdminMethod, AdminOrderedCallable,
49 49
     Report, MainPrepopulated, RelatedPrepopulated, UnorderedObject,
50 50
     Simple, UndeletableObject, UnchangeableObject, Choice, ShortMessage,
51  
-    Telegram, Pizza, Topping)
  51
+    Telegram, Pizza, Topping, FilteredManager)
52 52
 from .admin import site, site2
53 53
 
54 54
 
@@ -2656,6 +2656,12 @@ class AdminCustomQuerysetTest(TestCase):
2656 2656
     def setUp(self):
2657 2657
         self.client.login(username='super', password='secret')
2658 2658
         self.pks = [EmptyModel.objects.create().id for i in range(3)]
  2659
+        self.super_login = {
  2660
+            REDIRECT_FIELD_NAME: '/test_admin/admin/',
  2661
+            LOGIN_FORM_KEY: 1,
  2662
+            'username': 'super',
  2663
+            'password': 'secret',
  2664
+        }
2659 2665
 
2660 2666
     def test_changelist_view(self):
2661 2667
         response = self.client.get('/test_admin/admin/admin_views/emptymodel/')
@@ -2873,6 +2879,20 @@ def test_edit_model_modeladmin_only_qs(self):
2873 2879
             html=True
2874 2880
         )
2875 2881
 
  2882
+    def test_history_view_custom_qs(self):
  2883
+        """
  2884
+        Ensure that custom querysets are considered for the admin history view.
  2885
+        Refs #21013.
  2886
+        """
  2887
+        self.client.post('/test_admin/admin/', self.super_login)
  2888
+        FilteredManager.objects.create(pk=1)
  2889
+        FilteredManager.objects.create(pk=2)
  2890
+        response = self.client.get('/test_admin/admin/admin_views/filteredmanager/')
  2891
+        self.assertContains(response, "PK=1")
  2892
+        self.assertContains(response, "PK=2")
  2893
+        self.assertEqual(self.client.get('/test_admin/admin/admin_views/filteredmanager/1/history/').status_code, 200)
  2894
+        self.assertEqual(self.client.get('/test_admin/admin/admin_views/filteredmanager/2/history/').status_code, 200)
  2895
+
2876 2896
 
2877 2897
 @override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))
2878 2898
 class AdminInlineFileUploadTest(TestCase):

0 notes on commit 04415bf

Please sign in to comment.
Something went wrong with that request. Please try again.