Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #12654 - Correctly display ForeignKey fields in read-only scena…

…rios. Thanks, minmax.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12366 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 267346678d6fe2bbccf7394bae3c380af1a3a25c 1 parent 6c36e10
Jannis Leidel authored February 01, 2010
2  django/contrib/admin/util.py
@@ -247,7 +247,7 @@ def lookup_field(name, obj, model_admin=None):
247 247
         f = None
248 248
     else:
249 249
         attr = None
250  
-        value = getattr(obj, f.attname)
  250
+        value = getattr(obj, name)
251 251
     return f, attr, value
252 252
 
253 253
 def label_for_field(name, model, model_admin=None, return_attr=False):
1  tests/regressiontests/admin_util/models.py
@@ -6,6 +6,7 @@ class Article(models.Model):
6 6
     """
7 7
     A simple Article model for testing
8 8
     """
  9
+    site = models.ForeignKey('sites.Site', related_name="admin_articles")
9 10
     title = models.CharField(max_length=100)
10 11
     title2 = models.CharField(max_length=100, verbose_name="another name")
11 12
     created = models.DateTimeField()
47  tests/regressiontests/admin_util/tests.py
... ...
@@ -1,16 +1,61 @@
  1
+from datetime import datetime
1 2
 import unittest
2 3
 
3 4
 from django.db import models
4 5
 
5 6
 from django.contrib import admin
6  
-from django.contrib.admin.util import display_for_field, label_for_field
  7
+from django.contrib.admin.util import display_for_field, label_for_field, lookup_field
7 8
 from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE
  9
+from django.contrib.sites.models import Site
  10
+from django.utils.formats import localize
8 11
 
9 12
 from models import Article
10 13
 
11 14
 
12 15
 
13 16
 class UtilTests(unittest.TestCase):
  17
+    def test_values_from_lookup_field(self):
  18
+        """
  19
+        Regression test for #12654: lookup_field
  20
+        """
  21
+        SITE_NAME = 'example.com'
  22
+        TITLE_TEXT = 'Some title'
  23
+        CREATED_DATE = datetime.min
  24
+        ADMIN_METHOD = 'admin method'
  25
+        SIMPLE_FUNCTION = 'function'
  26
+        INSTANCE_ATTRIBUTE = 'attr'
  27
+
  28
+        class MockModelAdmin(object):
  29
+            def get_admin_value(self, obj):
  30
+                return ADMIN_METHOD
  31
+
  32
+        simple_function = lambda obj: SIMPLE_FUNCTION
  33
+
  34
+        article = Article(
  35
+            site=Site(domain=SITE_NAME),
  36
+            title=TITLE_TEXT,
  37
+            created=CREATED_DATE,
  38
+        )
  39
+        article.non_field = INSTANCE_ATTRIBUTE
  40
+
  41
+        verifications = (
  42
+            ('site', SITE_NAME),
  43
+            ('created', localize(CREATED_DATE)),
  44
+            ('title', TITLE_TEXT),
  45
+            ('get_admin_value', ADMIN_METHOD),
  46
+            (simple_function, SIMPLE_FUNCTION),
  47
+            ('test_from_model', article.test_from_model()),
  48
+            ('non_field', INSTANCE_ATTRIBUTE)
  49
+        )
  50
+
  51
+        mock_admin = MockModelAdmin()
  52
+        for name, value in verifications:
  53
+            field, attr, resolved_value = lookup_field(name, article, mock_admin)
  54
+
  55
+            if field is not None:
  56
+                resolved_value = display_for_field(resolved_value, field)
  57
+
  58
+            self.assertEqual(value, resolved_value)
14 59
 
15 60
     def test_null_display_for_field(self):
16 61
         """

0 notes on commit 2673466

Please sign in to comment.
Something went wrong with that request. Please try again.