Merge pull request #264 from noamkush/admin-prefetch-actor

Added prefetch for actor field in admin
This commit is contained in:
Federico Capoano 2019-12-19 14:19:21 +01:00 committed by GitHub
commit 0fba0345a7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 0 deletions

View file

@ -10,5 +10,9 @@ class NotificationAdmin(admin.ModelAdmin):
'level', 'target', 'unread', 'public')
list_filter = ('level', 'unread', 'public', 'timestamp',)
def get_queryset(self, request):
qs = super(NotificationAdmin, self).get_queryset(request)
return qs.prefetch_related('actor')
admin.site.register(Notification, NotificationAdmin)

View file

@ -11,8 +11,10 @@ import pytz
from django.conf import settings
from django.contrib.auth.models import Group, User
from django.core.exceptions import ImproperlyConfigured
from django.db import connection
from django.template import Context, Template
from django.test import RequestFactory, TestCase
from django.test.utils import CaptureQueriesContext
from django.utils import timezone
from django.utils.timezone import localtime, utc
from notifications.models import Notification, notify_handler
@ -510,3 +512,29 @@ class TagTest(TestCase):
context = {"user":self.to_user}
output = u"True"
self.tag_test(template, context, output)
class AdminTest(TestCase):
def setUp(self):
self.message_count = 10
self.from_user = User.objects.create_user(username="from", password="pwd", email="example@example.com")
self.to_user = User.objects.create_user(username="to", password="pwd", email="example@example.com")
self.to_user.is_staff = True
self.to_user.is_superuser = True
self.to_user.save()
for _ in range(self.message_count):
notify.send(
self.from_user,
recipient=self.to_user,
verb='commented',
action_object=self.from_user,
)
def test_list(self):
self.client.login(username='to', password='pwd')
with CaptureQueriesContext(connection=connection) as context:
response = self.client.get(reverse('admin:notifications_notification_changelist'))
self.assertLessEqual(len(context), 6)
self.assertEqual(response.status_code, 200, response.content)