Refs #8261 -- Fixed regression introduced by fd219fa.

ModelAdmin displayed the "View on site" link even if the Model didn't
define the `get_absolute_url()` method.
This commit is contained in:
Loic Bistuer 2013-11-21 16:26:49 +07:00
parent fafb6cf049
commit 8e670a0e50
3 changed files with 13 additions and 1 deletions

View File

@ -250,7 +250,7 @@ class BaseModelAdmin(six.with_metaclass(RenameBaseModelAdminMethods)):
if callable(self.view_on_site): if callable(self.view_on_site):
return self.view_on_site(obj) return self.view_on_site(obj)
elif self.view_on_site: elif self.view_on_site and hasattr(obj, 'get_absolute_url'):
# use the ContentType lookup if view_on_site is True # use the ContentType lookup if view_on_site is True
return reverse('admin:view_on_site', kwargs={ return reverse('admin:view_on_site', kwargs={
'content_type_id': ContentType.objects.get_for_model(obj).pk, 'content_type_id': ContentType.objects.get_for_model(obj).pk,

View File

@ -745,11 +745,17 @@ class City(models.Model):
state = models.ForeignKey(State) state = models.ForeignKey(State)
name = models.CharField(max_length=100) name = models.CharField(max_length=100)
def get_absolute_url(self):
return '/dummy/%s/' % self.pk
class Restaurant(models.Model): class Restaurant(models.Model):
city = models.ForeignKey(City) city = models.ForeignKey(City)
name = models.CharField(max_length=100) name = models.CharField(max_length=100)
def get_absolute_url(self):
return '/dummy/%s/' % self.pk
class Worker(models.Model): class Worker(models.Model):
work_at = models.ForeignKey(Restaurant) work_at = models.ForeignKey(Restaurant)

View File

@ -13,6 +13,7 @@ from django.core.exceptions import ImproperlyConfigured
from django.core.urlresolvers import reverse, NoReverseMatch from django.core.urlresolvers import reverse, NoReverseMatch
# Register auth models with the admin. # Register auth models with the admin.
from django.contrib.auth import get_permission_codename from django.contrib.auth import get_permission_codename
from django.contrib.admin import ModelAdmin
from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME
from django.contrib.admin.models import LogEntry, DELETION from django.contrib.admin.models import LogEntry, DELETION
from django.contrib.admin.sites import LOGIN_FORM_KEY from django.contrib.admin.sites import LOGIN_FORM_KEY
@ -4641,6 +4642,11 @@ class AdminViewOnSiteTest(TestCase):
'"/worker/%s/%s/"' % (worker.surname, worker.name), '"/worker/%s/%s/"' % (worker.surname, worker.name),
) )
def test_missing_get_absolute_url(self):
"Ensure None is returned if model doesn't have get_absolute_url"
model_admin = ModelAdmin(Worker, None)
self.assertIsNone(model_admin.get_view_on_site_url(Worker()))
@override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',)) @override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))
class InlineAdminViewOnSiteTest(TestCase): class InlineAdminViewOnSiteTest(TestCase):