Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Fixed #2856 -- Admin's Recent Actions will link to a 404 when an object ... #1144

Closed
wants to merge 1 commit into from

2 participants

Leszek Piątek Marc Tamlyn
Leszek Piątek
lechup commented

Fixed #2856 -- Admin's Recent Actions will link to a 404 when an object has been deleted.

Marc Tamlyn
Owner

Thanks for bringing this up to speed. Whilst the code looks plausible, I'm really not sure it's the best way of going about it. We're adding 4/5 queries to the admin home page which might not sound like a lot, but I don't want to introduce a feature like this if we can avoid it.

I've added some alternative approaches to the ticket.

Marc Tamlyn mjtamlyn closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

May 18, 2013
Leszek Piątek lechup Fixed #2856 -- Admin's Recent Actions will link to a 404 when an obje…
…ct has been deleted.
91de517
This page is out of date. Refresh to see the latest.
9 django/contrib/admin/models.py
@@ -5,6 +5,7 @@
5 5 from django.contrib.contenttypes.models import ContentType
6 6 from django.contrib.admin.util import quote
7 7 from django.core.urlresolvers import reverse
  8 +from django.core.exceptions import ObjectDoesNotExist
8 9 from django.utils.translation import ugettext, ugettext_lazy as _
9 10 from django.utils.encoding import smart_text
10 11 from django.utils.encoding import python_2_unicode_compatible
@@ -63,6 +64,14 @@ def is_change(self):
63 64 def is_deletion(self):
64 65 return self.action_flag == DELETION
65 66
  67 + def object_exists(self):
  68 + "Check whether object still exists in the database"
  69 + try:
  70 + self.get_edited_object()
  71 + except ObjectDoesNotExist:
  72 + return False
  73 + return True
  74 +
66 75 def get_edited_object(self):
67 76 "Returns the edited object represented by this log entry"
68 77 return self.content_type.get_object_for_this_type(pk=self.object_id)
6 django/contrib/admin/templates/admin/index.html
@@ -67,7 +67,11 @@
67 67 {% if entry.is_deletion or not entry.get_admin_url %}
68 68 {{ entry.object_repr }}
69 69 {% else %}
70   - <a href="{{ entry.get_admin_url }}">{{ entry.object_repr }}</a>
  70 + {% if entry.object_exists %}
  71 + <a href="{{ entry.get_admin_url }}">{{ entry.object_repr }}</a>
  72 + {% else %}
  73 + {{ entry.object_repr }}
  74 + {% endif %}
71 75 {% endif %}
72 76 <br/>
73 77 {% if entry.content_type %}
8 tests/admin_views/tests.py
@@ -17,7 +17,7 @@
17 17 # Register auth models with the admin.
18 18 from django.contrib import admin
19 19 from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME
20   -from django.contrib.admin.models import LogEntry, DELETION
  20 +from django.contrib.admin.models import LogEntry, DELETION, ADDITION
21 21 from django.contrib.admin.sites import LOGIN_FORM_KEY
22 22 from django.contrib.admin.util import quote
23 23 from django.contrib.admin.views.main import IS_POPUP_VAR
@@ -1042,6 +1042,9 @@ def testAddView(self):
1042 1042 self.assertEqual(Article.objects.all().count(), 4)
1043 1043 self.assertEqual(len(mail.outbox), 1)
1044 1044 self.assertEqual(mail.outbox[0].subject, 'Greetings from a created object')
  1045 + article_ct = ContentType.objects.get_for_model(Article)
  1046 + logged = LogEntry.objects.get(content_type=article_ct, action_flag=ADDITION)
  1047 + self.assertTrue(logged.object_exists())
1045 1048 self.client.get('/test_admin/admin/logout/')
1046 1049
1047 1050 # Super can add too, but is redirected to the change list view
@@ -1261,7 +1264,7 @@ def testDeleteView(self):
1261 1264 self.client.get('/test_admin/admin/')
1262 1265 self.client.post('/test_admin/admin/', self.deleteuser_login)
1263 1266 response = self.client.get('/test_admin/admin/admin_views/section/1/delete/')
1264   - # test response contains link to related Article
  1267 + # test response contains link to related Article
1265 1268 self.assertContains(response, "admin_views/article/1/")
1266 1269
1267 1270 response = self.client.get('/test_admin/admin/admin_views/article/1/delete/')
@@ -1274,6 +1277,7 @@ def testDeleteView(self):
1274 1277 article_ct = ContentType.objects.get_for_model(Article)
1275 1278 logged = LogEntry.objects.get(content_type=article_ct, action_flag=DELETION)
1276 1279 self.assertEqual(logged.object_id, '1')
  1280 + self.assertFalse(logged.object_exists())
1277 1281 self.client.get('/test_admin/admin/logout/')
1278 1282
1279 1283 def testDisabledPermissionsWhenLoggedIn(self):

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.