Ticket #12475: 12475_changelist_hidden_fields.2.diff

File 12475_changelist_hidden_fields.2.diff, 6.4 KB (added by Julien Phalip, 14 years ago)
  • django/contrib/admin/templates/admin/change_list_results.html

    diff --git a/django/contrib/admin/templates/admin/change_list_results.html b/django/contrib/admin/templates/admin/change_list_results.html
    index 6cfd9af..891268f 100644
    a b  
    11{% if result_hidden_fields %}
    2 <div class="hiddenfields"> {# DIV for HTML validation #}
     2<div class="hiddenfields">{# DIV for HTML validation #}
    33{% for item in result_hidden_fields %}{{ item }}{% endfor %}
    44</div>
    55{% endif %}
  • 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 bbe4b64..1981283 100644
    a b def items_for_result(cl, result, form):  
    188188            # By default the fields come from ModelAdmin.list_editable, but if we pull
    189189            # the fields out of the form instead of list_editable custom admins
    190190            # can provide fields on a per request basis
    191             if form and field_name in form.fields:
     191            if form and field_name in form.fields and \
     192                not (field_name == cl.model._meta.pk.name and form[cl.model._meta.pk.name].is_hidden):
    192193                bf = form[field_name]
    193194                result_repr = mark_safe(force_unicode(bf.errors) + force_unicode(bf))
    194195            else:
  • tests/regressiontests/admin_views/models.py

    diff --git a/tests/regressiontests/admin_views/models.py b/tests/regressiontests/admin_views/models.py
    index 9ac99cc..32fd3c8 100644
    a b class CoverLetterAdmin(admin.ModelAdmin):  
    735735        #return super(CoverLetterAdmin, self).queryset(request).only('author')
    736736        return super(CoverLetterAdmin, self).queryset(request).defer('date')
    737737
     738class Story(models.Model):
     739    title = models.CharField(max_length=100)
     740    content = models.TextField()
     741
     742class StoryForm(forms.ModelForm):
     743    class Meta:
     744        widgets = {'title': forms.HiddenInput}
     745   
     746class StoryAdmin(admin.ModelAdmin):
     747    list_display = ('id', 'title', 'content')
     748    list_display_links = ('title',) # 'id' not in list_display_links
     749    list_editable = ('content', )
     750    form = StoryForm
     751
     752class OtherStory(models.Model):
     753    title = models.CharField(max_length=100)
     754    content = models.TextField()
     755
     756class OtherStoryAdmin(admin.ModelAdmin):
     757    list_display = ('id', 'title', 'content')
     758    list_display_links = ('title', 'id') # 'id' in list_display_links
     759    list_editable = ('content', )
    738760
    739761admin.site.register(Article, ArticleAdmin)
    740762admin.site.register(CustomArticle, CustomArticleAdmin)
    admin.site.register(FoodDelivery, FoodDeliveryAdmin)  
    776798admin.site.register(RowLevelChangePermissionModel, RowLevelChangePermissionModelAdmin)
    777799admin.site.register(Paper, PaperAdmin)
    778800admin.site.register(CoverLetter, CoverLetterAdmin)
     801admin.site.register(Story, StoryAdmin)
     802admin.site.register(OtherStory, OtherStoryAdmin)
    779803
    780804# We intentionally register Promo and ChapterXtra1 but not Chapter nor ChapterXtra2.
    781805# 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 6be07b4..6c974cf 100644
    a b from models import (Article, BarAccount, CustomArticle, EmptyModel,  
    3636    Language, Collector, Widget, Grommet, DooHickey, FancyDoodad, Whatsit,
    3737    Category, Post, Plot, FunkyTag, Chapter, Book, Promo, WorkHour, Employee,
    3838    Question, Answer, Inquisition, Actor, FoodDelivery,
    39     RowLevelChangePermissionModel, Paper, CoverLetter)
     39    RowLevelChangePermissionModel, Paper, CoverLetter, Story, OtherStory)
    4040
    4141
    4242class AdminViewBasicTest(TestCase):
    class AdminViewListEditable(TestCase):  
    16781678        response = self.client.get('/test_admin/admin/admin_views/person/?%s' % IS_POPUP_VAR)
    16791679        self.assertEqual(response.context['cl'].list_editable, ())
    16801680
    1681 
     1681    def test_pk_hidden_fields(self):
     1682        """ Ensure that hidden pk fields aren't displayed in the table body and
     1683            that their corresponding human-readable value is displayed instead.
     1684            Note that the hidden pk fields are in fact be displayed but
     1685            separately (not in the table), and only once.
     1686            Refs #12475.
     1687        """
     1688        Story.objects.create(title='The adventures of Guido', content='Once upon a time in Djangoland...')
     1689        Story.objects.create(title='Crouching Tiger, Hidden Python', content='The Python was sneaking into...')
     1690        response = self.client.get('/test_admin/admin/admin_views/story/')
     1691        self.assertContains(response, 'id="id_form-0-id"', 1) # Only one hidden field, in a separate place than the table.
     1692        self.assertContains(response, 'id="id_form-1-id"', 1)
     1693        self.assertContains(response, '<div class="hiddenfields">\n<input type="hidden" name="form-0-id" value="2" id="id_form-0-id" /><input type="hidden" name="form-1-id" value="1" id="id_form-1-id" />\n</div>')
     1694        self.assertContains(response, '<td>1</td>', 1)
     1695        self.assertContains(response, '<td>2</td>', 1)
     1696
     1697    def test_pk_hidden_fields_with_list_display_links(self):
     1698        """ Similarly as test_pk_hidden_fields, but when the hidden pk fields are
     1699            referenced in list_display_links.
     1700            Refs #12475.
     1701        """
     1702        OtherStory.objects.create(title='The adventures of Guido', content='Once upon a time in Djangoland...')
     1703        OtherStory.objects.create(title='Crouching Tiger, Hidden Python', content='The Python was sneaking into...')
     1704        response = self.client.get('/test_admin/admin/admin_views/otherstory/')
     1705        self.assertContains(response, 'id="id_form-0-id"', 1) # Only one hidden field, in a separate place than the table.
     1706        self.assertContains(response, 'id="id_form-1-id"', 1)
     1707        self.assertContains(response, '<div class="hiddenfields">\n<input type="hidden" name="form-0-id" value="2" id="id_form-0-id" /><input type="hidden" name="form-1-id" value="1" id="id_form-1-id" />\n</div>')
     1708        self.assertContains(response, '<th><a href="1/">1</a></th>', 1)
     1709        self.assertContains(response, '<th><a href="2/">2</a></th>', 1)
     1710       
    16821711class AdminSearchTest(TestCase):
    16831712    fixtures = ['admin-views-users','multiple-child-classes']
    16841713
Back to Top