Ticket #21013: admin-history-get_queryset-1.diff

File admin-history-get_queryset-1.diff, 5.0 KB (added by Jonas H., 11 years ago)
  • django/contrib/admin/options.py

    diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
    index d435675..5d9a2f4 100644
    a b from django.forms.models import (modelform_factory, modelformset_factory,  
    2828    inlineformset_factory, BaseInlineFormSet, modelform_defines_fields)
    2929from django.http import Http404, HttpResponseRedirect
    3030from django.http.response import HttpResponseBase
    31 from django.shortcuts import get_object_or_404
    3231from django.template.response import SimpleTemplateResponse, TemplateResponse
    3332from django.utils.decorators import method_decorator
    3433from django.utils.html import escape, escapejs
    class ModelAdmin(BaseModelAdmin):  
    15171516        from django.contrib.admin.models import LogEntry
    15181517        # First check if the user can see this history.
    15191518        model = self.model
    1520         obj = get_object_or_404(model, pk=unquote(object_id))
     1519        obj = self.get_object(request, unquote(object_id))
     1520        if obj is None:
     1521            raise Http404('No %s matches the given query.' % model._meta.object_name)
    15211522
    15221523        if not self.has_change_permission(request, obj):
    15231524            raise PermissionDenied
  • tests/admin_views/admin.py

    diff --git a/tests/admin_views/admin.py b/tests/admin_views/admin.py
    index b2da23f..a3ce942 100644
    a b from .models import (Article, Chapter, Account, Media, Child, Parent, Picture,  
    2929    Album, Question, Answer, ComplexSortedPerson, PluggableSearchPerson, PrePopulatedPostLargeSlug,
    3030    AdminOrderedField, AdminOrderedModelMethod, AdminOrderedAdminMethod,
    3131    AdminOrderedCallable, Report, Color2, UnorderedObject, MainPrepopulated,
    32     RelatedPrepopulated, UndeletableObject, UnchangeableObject, UserMessenger, Simple, Choice,
    33     ShortMessage, Telegram)
     32    RelatedPrepopulated, UndeletableObject, UnchangeableObject, UserMessenger,
     33    Simple, FilteredManager, Choice, ShortMessage, Telegram)
    3434
    3535
    3636def callable_year(dt_value):
    def callable_on_unknown(obj):  
    674674class AttributeErrorRaisingAdmin(admin.ModelAdmin):
    675675    list_display = [callable_on_unknown, ]
    676676
     677class CustomManagerAdmin(admin.ModelAdmin):
     678    def get_queryset(self, request):
     679        return FilteredManager.objects
     680
    677681class MessageTestingAdmin(admin.ModelAdmin):
    678682    actions = ["message_debug", "message_info", "message_success",
    679683               "message_warning", "message_error", "message_extra_tags"]
    site.register(AdminOrderedAdminMethod, AdminOrderedAdminMethodAdmin)  
    781785site.register(AdminOrderedCallable, AdminOrderedCallableAdmin)
    782786site.register(Color2, CustomTemplateFilterColorAdmin)
    783787site.register(Simple, AttributeErrorRaisingAdmin)
     788site.register(FilteredManager, CustomManagerAdmin)
    784789site.register(UserMessenger, MessageTestingAdmin)
    785790site.register(Choice, ChoiceList)
    786791
  • tests/admin_views/models.py

    diff --git a/tests/admin_views/models.py b/tests/admin_views/models.py
    index 6483fc8..40336c1 100644
    a b class Simple(models.Model):  
    690690    Simple model with nothing on it for use in testing
    691691    """
    692692
     693class _Manager(models.Manager):
     694    def get_queryset(self):
     695        return super(_Manager, self).get_queryset().filter(pk__gt=1)
     696
     697class FilteredManager(models.Model):
     698    def __str__(self):
     699        return "PK=%d" % self.pk
     700
     701    pk_gt_1 = _Manager()
     702    objects = models.Manager()
     703
    693704class Choice(models.Model):
    694705    choice = models.IntegerField(blank=True, null=True,
    695706        choices=((1, 'Yes'), (0, 'No'), (None, 'No opinion')))
  • tests/admin_views/tests.py

    diff --git a/tests/admin_views/tests.py b/tests/admin_views/tests.py
    index aa5f9ec..2873cae 100644
    a b from .models import (Article, BarAccount, CustomArticle, EmptyModel, FooAccount,  
    5151    AdminOrderedModelMethod, AdminOrderedAdminMethod, AdminOrderedCallable,
    5252    Report, MainPrepopulated, RelatedPrepopulated, UnorderedObject,
    5353    Simple, UndeletableObject, UnchangeableObject, Choice, ShortMessage,
    54     Telegram, Pizza, Topping)
     54    Telegram, Pizza, Topping, FilteredManager)
    5555from .admin import site, site2
    5656
    5757
    class AdminViewPermissionsTest(TestCase):  
    12121212
    12131213            self.client.get('/test_admin/admin/logout/')
    12141214
     1215    def testHistoryViewCustomQueryset(self):
     1216        self.client.post('/test_admin/admin/', self.super_login)
     1217        FilteredManager.objects.create(pk=1)
     1218        FilteredManager.objects.create(pk=2)
     1219        response = self.client.get('/test_admin/admin/admin_views/filteredmanager/')
     1220        self.assertContains(response, "PK=1")
     1221        self.assertContains(response, "PK=2")
     1222        self.assertEqual(self.client.get('/test_admin/admin/admin_views/filteredmanager/1/history/').status_code, 200)
     1223        self.assertEqual(self.client.get('/test_admin/admin/admin_views/filteredmanager/2/history/').status_code, 200)
     1224
    12151225    def testConditionallyShowAddSectionLink(self):
    12161226        """
    12171227        The foreign key widget should only show the "add related" button if the
Back to Top