Author: russellm
Date: 2011-01-24 01:01:00 -0600 (Mon, 24 Jan 2011)
New Revision: 15286

Modified:
   django/trunk/django/contrib/admin/templatetags/admin_list.py
   django/trunk/tests/regressiontests/admin_changelist/models.py
   django/trunk/tests/regressiontests/admin_changelist/tests.py
Log:
Fixed #14982 -- Ensure that EMPTY_CHANGELIST_VALUE is honored for nullable 
foreign keys. Thanks to marcob for the report and fix, and to sontek for the 
test case.

Modified: django/trunk/django/contrib/admin/templatetags/admin_list.py
===================================================================
--- django/trunk/django/contrib/admin/templatetags/admin_list.py        
2011-01-24 06:36:31 UTC (rev 15285)
+++ django/trunk/django/contrib/admin/templatetags/admin_list.py        
2011-01-24 07:01:00 UTC (rev 15286)
@@ -156,7 +156,11 @@
                 if value is None:
                     result_repr = EMPTY_CHANGELIST_VALUE
                 if isinstance(f.rel, models.ManyToOneRel):
-                    result_repr = escape(getattr(result, f.name))
+                    field_val = getattr(result, f.name)
+                    if field_val is None:
+                        result_repr = EMPTY_CHANGELIST_VALUE
+                    else:
+                        result_repr = escape(field_val)
                 else:
                     result_repr = display_for_field(value, f)
                 if isinstance(f, models.DateField) or isinstance(f, 
models.TimeField):

Modified: django/trunk/tests/regressiontests/admin_changelist/models.py
===================================================================
--- django/trunk/tests/regressiontests/admin_changelist/models.py       
2011-01-24 06:36:31 UTC (rev 15285)
+++ django/trunk/tests/regressiontests/admin_changelist/models.py       
2011-01-24 07:01:00 UTC (rev 15286)
@@ -5,5 +5,5 @@
     name = models.CharField(max_length=128)
 
 class Child(models.Model):
-    parent = models.ForeignKey(Parent, editable=False)
-    name = models.CharField(max_length=30, blank=True)
\ No newline at end of file
+    parent = models.ForeignKey(Parent, editable=False, null=True)
+    name = models.CharField(max_length=30, blank=True)

Modified: django/trunk/tests/regressiontests/admin_changelist/tests.py
===================================================================
--- django/trunk/tests/regressiontests/admin_changelist/tests.py        
2011-01-24 06:36:31 UTC (rev 15285)
+++ django/trunk/tests/regressiontests/admin_changelist/tests.py        
2011-01-24 07:01:00 UTC (rev 15286)
@@ -20,6 +20,26 @@
                 m.list_select_related, m.list_per_page, m.list_editable, m)
         self.assertEqual(cl.query_set.query.select_related, {'parent': 
{'name': {}}})
 
+    def test_result_list_empty_changelist_value(self):
+        """
+        Regression test for #14982: EMPTY_CHANGELIST_VALUE should be honored
+        for relationship fields
+        """
+        new_child = Child.objects.create(name='name', parent=None)
+        request = MockRequest()
+        m = ChildAdmin(Child, admin.site)
+        cl = ChangeList(request, Child, m.list_display, m.list_display_links,
+                m.list_filter, m.date_hierarchy, m.search_fields,
+                m.list_select_related, m.list_per_page, m.list_editable, m)
+        cl.formset = None
+        template = Template('{% load admin_list %}{% spaceless %}{% 
result_list cl %}{% endspaceless %}')
+        context = Context({'cl': cl})
+        table_output = template.render(context)
+        row_html = '<tbody><tr class="row1"><td><input type="checkbox" 
class="action-select" value="1" name="_selected_action" /></td><th><a 
href="1/">name</a></th><td>(None)</td></tr></tbody>'
+        self.assertFalse(table_output.find(row_html) == -1,
+            'Failed to find expected row element: %s' % table_output)
+
+
     def test_result_list_html(self):
         """
         Verifies that inclusion tag result_list generates a table when with

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-updates@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to