Ticket #9969: 9969-admin-bugs-with-nested-choices-r9905.diff

File 9969-admin-bugs-with-nested-choices-r9905.diff, 6.4 KB (added by Ramiro Morales, 16 years ago)

Updated patch includes fixes for poblems in changelist tabel and in filter section, includes tests

  • django/contrib/admin/filterspecs.py

    diff -r 3976471d3430 django/contrib/admin/filterspecs.py
    a b  
    8989        yield {'selected': self.lookup_val is None,
    9090               'query_string': cl.get_query_string({}, [self.lookup_kwarg]),
    9191               'display': _('All')}
    92         for k, v in self.field.choices:
     92        for k, v in self.field.flatchoices:
    9393            yield {'selected': smart_unicode(k) == self.lookup_val,
    9494                    'query_string': cl.get_query_string({self.lookup_kwarg: k}),
    9595                    'display': v}
  • django/contrib/admin/templatetags/admin_list.py

    diff -r 3976471d3430 django/contrib/admin/templatetags/admin_list.py
    a b  
    205205                    result_repr = EMPTY_CHANGELIST_VALUE
    206206            # Fields with choices are special: Use the representation
    207207            # of the choice.
    208             elif f.choices:
    209                 result_repr = dict(f.choices).get(field_val, EMPTY_CHANGELIST_VALUE)
     208            elif f.flatchoices:
     209                result_repr = dict(f.flatchoices).get(field_val, EMPTY_CHANGELIST_VALUE)
    210210            else:
    211211                result_repr = escape(field_val)
    212212        if force_unicode(result_repr) == '':
  • tests/regressiontests/admin_views/customadmin.py

    diff -r 3976471d3430 tests/regressiontests/admin_views/customadmin.py
    a b  
    2828site.register(models.Article, models.ArticleAdmin)
    2929site.register(models.Section, inlines=[models.ArticleInline])
    3030site.register(models.Thing, models.ThingAdmin)
     31site.register(models.Fabric, models.FabricAdmin)
  • new file tests/regressiontests/admin_views/fixtures/admin-views-fabrics.xml

    diff -r 3976471d3430 tests/regressiontests/admin_views/fixtures/admin-views-fabrics.xml
    - +  
     1<?xml version="1.0" encoding="utf-8"?>
     2<django-objects version="1.0">
     3  <object pk="1" model="admin_views.fabric">
     4    <field type="CharField" name="surface">x</field>
     5  </object>
     6  <object pk="2" model="admin_views.fabric">
     7    <field type="CharField" name="surface">y</field>
     8  </object>
     9  <object pk="3" model="admin_views.fabric">
     10    <field type="CharField" name="surface">plain</field>
     11  </object>
     12</django-objects>
  • tests/regressiontests/admin_views/models.py

    diff -r 3976471d3430 tests/regressiontests/admin_views/models.py
    a b  
    134134class ThingAdmin(admin.ModelAdmin):
    135135    list_filter = ('color',)
    136136
     137class Fabric(models.Model):
     138    NG_CHOICES = (
     139        ('Textured', (
     140                ('x', 'Horizontal'),
     141                ('y', 'Vertical'),
     142            )
     143        ),
     144        ('plain', 'Smooth'),
     145    )
     146    surface = models.CharField(max_length=20, choices=NG_CHOICES)
     147
     148class FabricAdmin(admin.ModelAdmin):
     149    list_display = ('surface',)
     150    list_filter = ('surface',)
     151
    137152admin.site.register(Article, ArticleAdmin)
    138153admin.site.register(CustomArticle, CustomArticleAdmin)
    139154admin.site.register(Section, inlines=[ArticleInline])
    140155admin.site.register(ModelWithStringPrimaryKey)
    141156admin.site.register(Color)
    142157admin.site.register(Thing, ThingAdmin)
     158admin.site.register(Fabric, FabricAdmin)
    143159
    144160# We intentionally register Promo and ChapterXtra1 but not Chapter nor ChapterXtra2.
    145161# That way we cover all four cases:
  • tests/regressiontests/admin_views/tests.py

    diff -r 3976471d3430 tests/regressiontests/admin_views/tests.py
    a b  
    1212from models import Article, CustomArticle, Section, ModelWithStringPrimaryKey
    1313
    1414class AdminViewBasicTest(TestCase):
    15     fixtures = ['admin-views-users.xml', 'admin-views-colors.xml']
    16    
    17     # Store the bit of the URL where the admin is registered as a class 
     15    fixtures = ['admin-views-users.xml', 'admin-views-colors.xml', 'admin-views-fabrics.xml']
     16
     17    # Store the bit of the URL where the admin is registered as a class
    1818    # variable. That way we can test a second AdminSite just by subclassing
    1919    # this test case and changing urlbit.
    2020    urlbit = 'admin'
     
    172172        self.assertRedirects(response, '/test_admin/%s/admin_views/thing/?e=1' % self.urlbit)       
    173173        response = self.client.get('/test_admin/%s/admin_views/thing/' % self.urlbit, {'color__id__exact': 'StringNotInteger!'})
    174174        self.assertRedirects(response, '/test_admin/%s/admin_views/thing/?e=1' % self.urlbit)
     175
     176    def testNamedGroupFieldChoicesChangeList(self):
     177        """
     178        Ensures the admin changelist shows correct values in the relevant column
     179        for rows corresponding to instances of a model in which a named group
     180        has been used in the choices option of a field.
     181        """
     182        response = self.client.get('/test_admin/%s/admin_views/fabric/' % self.urlbit)
     183        self.failUnlessEqual(response.status_code, 200)
     184        self.failUnless(
     185            '<a href="1/">Horizontal</a>' in response.content and
     186            '<a href="2/">Vertical</a>' in response.content,
     187            "Changelist table isn't showing the right human-readable values set by a model field 'choices' option named group."
     188        )
     189
     190    def testNamedGroupFieldChoicesFilter(self):
     191        """
     192        Ensures the filter UI shows correctly when at least one named group has
     193        been used in the choices option of a model field.
     194        """
     195        response = self.client.get('/test_admin/%s/admin_views/fabric/' % self.urlbit)
     196        self.failUnlessEqual(response.status_code, 200)
     197        self.failUnless(
     198            '<div id="changelist-filter">' in response.content,
     199            "Expected filter not found in changelist view."
     200        )
     201        self.failUnless(
     202            '<a href="?surface__exact=x">Horizontal</a>' in response.content and
     203            '<a href="?surface__exact=y">Vertical</a>' in response.content,
     204            "Changelist filter isn't showing options contained inside a model field 'choices' option named group."
     205        )
    175206
    176207class CustomModelAdminTest(AdminViewBasicTest):
    177208    urlbit = "admin2"
Back to Top