Ticket #12475: 12475_changelist_hidden_fields.diff

File 12475_changelist_hidden_fields.diff, 5.0 KB (added by Julien Phalip, 14 years ago)
  • django/contrib/admin/templatetags/admin_list.py

    diff --git a/django/contrib/admin/templatetags/admin_list.py b/django/contrib/admin/templatetags/admin_list.py
    index 806be24..133e0fc 100644
    a b def items_for_result(cl, result, form):  
    186186            # By default the fields come from ModelAdmin.list_editable, but if we pull
    187187            # the fields out of the form instead of list_editable custom admins
    188188            # can provide fields on a per request basis
    189             if form and field_name in form.fields:
     189            if form and field_name in form.fields and not form[field_name].is_hidden:
    190190                bf = form[field_name]
    191191                result_repr = mark_safe(force_unicode(bf.errors) + force_unicode(bf))
    192192            else:
  • tests/regressiontests/admin_views/models.py

    diff --git a/tests/regressiontests/admin_views/models.py b/tests/regressiontests/admin_views/models.py
    index 60319ea..1890674 100644
    a b class Answer(models.Model):  
    669669class Reservation(models.Model):
    670670    start_date = models.DateTimeField()
    671671    price = models.IntegerField()
     672
     673class Story(models.Model):
     674    title = models.CharField(max_length=100)
     675    content = models.TextField()
     676   
     677class StoryAdmin(admin.ModelAdmin):
     678    list_display = ('id', 'title', 'content')
     679    list_display_links = ('title',) # 'id' not in list_display_links
     680    list_editable = ('content', )
     681
     682class OtherStory(models.Model):
     683    title = models.CharField(max_length=100)
     684    content = models.TextField()
     685   
     686class OtherStoryAdmin(admin.ModelAdmin):
     687    list_display = ('id', 'title', 'content')
     688    list_display_links = ('title', 'id') # 'id' in list_display_links
     689    list_editable = ('content', )
    672690   
    673691admin.site.register(Article, ArticleAdmin)
    674692admin.site.register(CustomArticle, CustomArticleAdmin)
    admin.site.register(CyclicOne)  
    706724admin.site.register(CyclicTwo)
    707725admin.site.register(WorkHour, WorkHourAdmin)
    708726admin.site.register(Reservation)
     727admin.site.register(Story, StoryAdmin)
     728admin.site.register(OtherStory, OtherStoryAdmin)
    709729
    710730# We intentionally register Promo and ChapterXtra1 but not Chapter nor ChapterXtra2.
    711731# That way we cover all four cases:
  • tests/regressiontests/admin_views/tests.py

    diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py
    index acbbbfc..b3c691a 100644
    a b from models import (Article, BarAccount, CustomArticle, EmptyModel,  
    3535    Person, Persona, Picture, Podcast, Section, Subscriber, Vodcast,
    3636    Language, Collector, Widget, Grommet, DooHickey, FancyDoodad, Whatsit,
    3737    Category, Post, Plot, FunkyTag, Chapter, Book, Promo, WorkHour, Employee,
    38     Question, Answer, Inquisition, Actor)
     38    Question, Answer, Inquisition, Actor, Story, OtherStory)
    3939
    4040
    4141class AdminViewBasicTest(TestCase):
    class AdminViewListEditable(TestCase):  
    15631563        response = self.client.get('/test_admin/admin/admin_views/person/?%s' % IS_POPUP_VAR)
    15641564        self.assertEqual(response.context['cl'].list_editable, ())
    15651565
    1566 
     1566    def test_hidden_fields(self):
     1567        """ Ensure that hidden fields aren't displayed in the table body and
     1568            that their corresponding human-readable value is displayed instead.
     1569            Note that hidden fields should in fact be displayed but separately
     1570            (not in the table) and only once.
     1571            Refs #12475.
     1572        """
     1573        Story.objects.create(title='The adventures of Guido', content='Once upon a time in Djangoland...')
     1574        Story.objects.create(title='Crouching Tiger, Hidden Python', content='The Python was sneaking into...')
     1575        response = self.client.get('/test_admin/admin/admin_views/story/')
     1576        self.assertContains(response, 'id="id_form-0-id"', 1) # Only one hidden field, in a separate place than the table.
     1577        self.assertContains(response, 'id="id_form-1-id"', 1)
     1578        self.assertContains(response, '<td>1</td>', 1)
     1579        self.assertContains(response, '<td>2</td>', 1)
     1580
     1581    def test_hidden_fields_with_list_display_links(self):
     1582        """ Similarly as test_hidden_fields, but when the hidden fields are
     1583            referenced in list_display_links.
     1584            Refs #12475.
     1585        """
     1586        OtherStory.objects.create(title='The adventures of Guido', content='Once upon a time in Djangoland...')
     1587        OtherStory.objects.create(title='Crouching Tiger, Hidden Python', content='The Python was sneaking into...')
     1588        response = self.client.get('/test_admin/admin/admin_views/otherstory/')
     1589        self.assertContains(response, 'id="id_form-0-id"', 1) # Only one hidden field, in a separate place than the table.
     1590        self.assertContains(response, 'id="id_form-1-id"', 1)
     1591        self.assertContains(response, '<th><a href="1/">1</a></th>', 1)
     1592        self.assertContains(response, '<th><a href="2/">2</a></th>', 1)
     1593       
    15671594class AdminSearchTest(TestCase):
    15681595    fixtures = ['admin-views-users','multiple-child-classes']
    15691596
Back to Top