Merge pull request #1954 from loic/ticket8261

Refs #8261 -- Fixed regression introduced by fd219fa.
This commit is contained in:
Anssi Kääriäinen 2013-11-21 01:35:21 -08:00
commit 1116a5662c
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):
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
return reverse('admin:view_on_site', kwargs={
'content_type_id': ContentType.objects.get_for_model(obj).pk,

View File

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

View File

@ -13,6 +13,7 @@ from django.core.exceptions import ImproperlyConfigured
from django.core.urlresolvers import reverse, NoReverseMatch
# Register auth models with the admin.
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.models import LogEntry, DELETION
from django.contrib.admin.sites import LOGIN_FORM_KEY
@ -4641,6 +4642,11 @@ class AdminViewOnSiteTest(TestCase):
'"/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',))
class InlineAdminViewOnSiteTest(TestCase):