From 8b050cf9dcad3db39cc9ef44906bfc39f5aa3d25 Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Mon, 12 Sep 2016 19:13:00 -0700 Subject: [PATCH] Refs #26524 -- Added a test for a _id reference in ModelAdmin.list_display. --- tests/admin_views/tests.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/tests/admin_views/tests.py b/tests/admin_views/tests.py index ccc38920d0..fd8056513b 100644 --- a/tests/admin_views/tests.py +++ b/tests/admin_views/tests.py @@ -52,9 +52,9 @@ from .models import ( CyclicOne, CyclicTwo, DooHickey, Employee, EmptyModel, ExternalSubscriber, Fabric, FancyDoodad, FieldOverridePost, FilteredManager, FooAccount, FoodDelivery, FunkyTag, Gallery, Grommet, Inquisition, Language, Link, - MainPrepopulated, ModelWithStringPrimaryKey, OtherStory, Paper, Parent, - ParentWithDependentChildren, ParentWithUUIDPK, Person, Persona, Picture, - Pizza, Plot, PlotDetails, PluggableSearchPerson, Podcast, Post, + MainPrepopulated, Media, ModelWithStringPrimaryKey, OtherStory, Paper, + Parent, ParentWithDependentChildren, ParentWithUUIDPK, Person, Persona, + Picture, Pizza, Plot, PlotDetails, PluggableSearchPerson, Podcast, Post, PrePopulatedPost, Promo, Question, Recommendation, Recommender, RelatedPrepopulated, RelatedWithUUIDPKModel, Report, Restaurant, RowLevelChangePermissionModel, SecretHideout, Section, ShortMessage, @@ -539,7 +539,7 @@ class AdminViewBasicTest(AdminViewBasicTestCase): self.assertContentBefore(response, 'The First Item', 'The Middle Item') self.assertContentBefore(response, 'The Middle Item', 'The Last Item') - def test_has_related_field_in_list_display(self): + def test_has_related_field_in_list_display_fk(self): """Joins shouldn't be performed for _id fields in list display.""" state = State.objects.create(name='Karnataka') City.objects.create(state=state, name='Bangalore') @@ -551,6 +551,18 @@ class AdminViewBasicTest(AdminViewBasicTestCase): response.context['cl'].list_display = ['id', 'name', 'state_id'] self.assertIs(response.context['cl'].has_related_field_in_list_display(), False) + def test_has_related_field_in_list_display_o2o(self): + """Joins shouldn't be performed for _id fields in list display.""" + media = Media.objects.create(name='Foo') + Vodcast.objects.create(media=media) + response = self.client.get(reverse('admin:admin_views_vodcast_changelist'), {}) + + response.context['cl'].list_display = ['media'] + self.assertIs(response.context['cl'].has_related_field_in_list_display(), True) + + response.context['cl'].list_display = ['media_id'] + self.assertIs(response.context['cl'].has_related_field_in_list_display(), False) + def test_limited_filter(self): """Ensure admin changelist filters do not contain objects excluded via limit_choices_to. This also tests relation-spanning filters (e.g. 'color__value').