From 267346678d6fe2bbccf7394bae3c380af1a3a25c Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Mon, 1 Feb 2010 14:12:56 +0000 Subject: [PATCH] Fixed #12654 - Correctly display ForeignKey fields in read-only scenarios. Thanks, minmax. git-svn-id: http://code.djangoproject.com/svn/django/trunk@12366 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/admin/util.py | 2 +- tests/regressiontests/admin_util/models.py | 1 + tests/regressiontests/admin_util/tests.py | 47 +++++++++++++++++++++- 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/django/contrib/admin/util.py b/django/contrib/admin/util.py index d252a805ab..bc77226720 100644 --- a/django/contrib/admin/util.py +++ b/django/contrib/admin/util.py @@ -247,7 +247,7 @@ def lookup_field(name, obj, model_admin=None): f = None else: attr = None - value = getattr(obj, f.attname) + value = getattr(obj, name) return f, attr, value def label_for_field(name, model, model_admin=None, return_attr=False): diff --git a/tests/regressiontests/admin_util/models.py b/tests/regressiontests/admin_util/models.py index c4022b3814..0c2af585cd 100644 --- a/tests/regressiontests/admin_util/models.py +++ b/tests/regressiontests/admin_util/models.py @@ -6,6 +6,7 @@ class Article(models.Model): """ A simple Article model for testing """ + site = models.ForeignKey('sites.Site', related_name="admin_articles") title = models.CharField(max_length=100) title2 = models.CharField(max_length=100, verbose_name="another name") created = models.DateTimeField() diff --git a/tests/regressiontests/admin_util/tests.py b/tests/regressiontests/admin_util/tests.py index e7f163aa94..479cfdcb52 100644 --- a/tests/regressiontests/admin_util/tests.py +++ b/tests/regressiontests/admin_util/tests.py @@ -1,16 +1,61 @@ +from datetime import datetime import unittest from django.db import models from django.contrib import admin -from django.contrib.admin.util import display_for_field, label_for_field +from django.contrib.admin.util import display_for_field, label_for_field, lookup_field from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE +from django.contrib.sites.models import Site +from django.utils.formats import localize from models import Article class UtilTests(unittest.TestCase): + def test_values_from_lookup_field(self): + """ + Regression test for #12654: lookup_field + """ + SITE_NAME = 'example.com' + TITLE_TEXT = 'Some title' + CREATED_DATE = datetime.min + ADMIN_METHOD = 'admin method' + SIMPLE_FUNCTION = 'function' + INSTANCE_ATTRIBUTE = 'attr' + + class MockModelAdmin(object): + def get_admin_value(self, obj): + return ADMIN_METHOD + + simple_function = lambda obj: SIMPLE_FUNCTION + + article = Article( + site=Site(domain=SITE_NAME), + title=TITLE_TEXT, + created=CREATED_DATE, + ) + article.non_field = INSTANCE_ATTRIBUTE + + verifications = ( + ('site', SITE_NAME), + ('created', localize(CREATED_DATE)), + ('title', TITLE_TEXT), + ('get_admin_value', ADMIN_METHOD), + (simple_function, SIMPLE_FUNCTION), + ('test_from_model', article.test_from_model()), + ('non_field', INSTANCE_ATTRIBUTE) + ) + + mock_admin = MockModelAdmin() + for name, value in verifications: + field, attr, resolved_value = lookup_field(name, article, mock_admin) + + if field is not None: + resolved_value = display_for_field(resolved_value, field) + + self.assertEqual(value, resolved_value) def test_null_display_for_field(self): """