Code

Ticket #14982: empty_changelist_value.patch

File empty_changelist_value.patch, 2.9 KB (added by sontek, 3 years ago)

fixes the issue and has a unit test

  • django/contrib/admin/templatetags/admin_list.py

     
    155155            else: 
    156156                if value is None: 
    157157                    result_repr = EMPTY_CHANGELIST_VALUE 
    158                 if isinstance(f.rel, models.ManyToOneRel): 
     158                elif isinstance(f.rel, models.ManyToOneRel): 
    159159                    result_repr = escape(getattr(result, f.name)) 
    160160                else: 
    161161                    result_repr = display_for_field(value, f) 
  • tests/regressiontests/admin_changelist/tests.py

     
    2020                m.list_select_related, m.list_per_page, m.list_editable, m) 
    2121        self.assertEqual(cl.query_set.query.select_related, {'parent': {'name': {}}}) 
    2222 
     23    def test_result_list_empty_changelist_value(self): 
     24        """ 
     25        Regression test for #14982: EMPTY_CHANGELIST_VALUE should be honored 
     26        for relationship fields 
     27        """ 
     28        new_child = Child.objects.create(name='name', parent=None) 
     29        request = MockRequest() 
     30        m = ChildAdmin(Child, admin.site) 
     31        cl = ChangeList(request, Child, m.list_display, m.list_display_links, 
     32                m.list_filter, m.date_hierarchy, m.search_fields, 
     33                m.list_select_related, m.list_per_page, m.list_editable, m) 
     34        cl.formset = None 
     35        template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}') 
     36        context = Context({'cl': cl}) 
     37        table_output = template.render(context) 
     38        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>' 
     39        self.assertFalse(table_output.find(row_html) == -1, 
     40            'Failed to find expected row element: %s' % table_output) 
     41 
     42 
    2343    def test_result_list_html(self): 
    2444        """ 
    2545        Verifies that inclusion tag result_list generates a table when with 
  • tests/regressiontests/admin_changelist/models.py

     
    55    name = models.CharField(max_length=128) 
    66 
    77class Child(models.Model): 
    8     parent = models.ForeignKey(Parent, editable=False) 
    9     name = models.CharField(max_length=30, blank=True) 
    10  No newline at end of file 
     8    parent = models.ForeignKey(Parent, editable=False, null=True) 
     9    name = models.CharField(max_length=30, blank=True)