1. Luke Plant
  2. django

Commits

ram...@bcc190cf-cafb-0310-a4f2-bffc1f526a37  committed d28464d

[1.2.X] Fixed #14529 -- Fixed representation of model names in admin messages after model object changes when the ModelAdmin queryset() uses defer() or only(). Thanks rlaager for report and initial patch, to rasca an julien for help in tracking the problem.

Backport of [15596] from trunk.

  • Participants
  • Parent commits 028b26f
  • Branches releases/1.2.X

Comments (0)

Files changed (3)

File django/contrib/admin/options.py

View file
  • Ignore whitespace
         Determines the HttpResponse for the change_view stage.
         """
         opts = obj._meta
+
+        # Handle proxy models automatically created by .only() or .defer()
+        verbose_name = opts.verbose_name
+        if obj._deferred:
+            opts_ = opts.proxy_for_model._meta
+            verbose_name = opts_.verbose_name
+
         pk_value = obj._get_pk_val()
 
-        msg = _('The %(name)s "%(obj)s" was changed successfully.') % {'name': force_unicode(opts.verbose_name), 'obj': force_unicode(obj)}
+        msg = _('The %(name)s "%(obj)s" was changed successfully.') % {'name': force_unicode(verbose_name), 'obj': force_unicode(obj)}
         if request.POST.has_key("_continue"):
             self.message_user(request, msg + ' ' + _("You may edit it again below."))
             if request.REQUEST.has_key('_popup'):
             else:
                 return HttpResponseRedirect(request.path)
         elif request.POST.has_key("_saveasnew"):
-            msg = _('The %(name)s "%(obj)s" was added successfully. You may edit it again below.') % {'name': force_unicode(opts.verbose_name), 'obj': obj}
+            msg = _('The %(name)s "%(obj)s" was added successfully. You may edit it again below.') % {'name': force_unicode(verbose_name), 'obj': obj}
             self.message_user(request, msg)
             return HttpResponseRedirect("../%s/" % pk_value)
         elif request.POST.has_key("_addanother"):
-            self.message_user(request, msg + ' ' + (_("You may add another %s below.") % force_unicode(opts.verbose_name)))
+            self.message_user(request, msg + ' ' + (_("You may add another %s below.") % force_unicode(verbose_name)))
             return HttpResponseRedirect("../add/")
         else:
             self.message_user(request, msg)

File tests/regressiontests/admin_views/models.py

View file
  • Ignore whitespace
     list_display=('reference', 'driver', 'restaurant')
     list_editable = ('driver', 'restaurant')
 
+class Paper(models.Model):
+    title = models.CharField(max_length=30)
+    author = models.CharField(max_length=30, blank=True, null=True)
+
+class CoverLetter(models.Model):
+    author = models.CharField(max_length=30)
+    date = models.DateField(null=True, blank=True)
+
+    def __unicode__(self):
+        return self.author
+
+class PaperAdmin(admin.ModelAdmin):
+    """
+    A ModelAdin with a custom queryset() method that uses only(), to test
+    verbose_name display in messages shown after adding Paper instances.
+    """
+
+    def queryset(self, request):
+        return super(PaperAdmin, self).queryset(request).only('title')
+
+class CoverLetterAdmin(admin.ModelAdmin):
+    """
+    A ModelAdin with a custom queryset() method that uses only(), to test
+    verbose_name display in messages shown after adding CoverLetter instances.
+    Note that the CoverLetter model defines a __unicode__ method.
+    """
+
+    def queryset(self, request):
+        #return super(CoverLetterAdmin, self).queryset(request).only('author')
+        return super(CoverLetterAdmin, self).queryset(request).defer('date')
+
 
 admin.site.register(Article, ArticleAdmin)
 admin.site.register(CustomArticle, CustomArticleAdmin)
 admin.site.register(Reservation)
 admin.site.register(FoodDelivery, FoodDeliveryAdmin)
 admin.site.register(RowLevelChangePermissionModel, RowLevelChangePermissionModelAdmin)
+admin.site.register(Paper, PaperAdmin)
+admin.site.register(CoverLetter, CoverLetterAdmin)
 
 # We intentionally register Promo and ChapterXtra1 but not Chapter nor ChapterXtra2.
 # That way we cover all four cases:

File tests/regressiontests/admin_views/tests.py

View file
  • Ignore whitespace
     Person, Persona, Picture, Podcast, Section, Subscriber, Vodcast,
     Language, Collector, Widget, Grommet, DooHickey, FancyDoodad, Whatsit,
     Category, Post, Plot, FunkyTag, WorkHour, Employee, Inquisition,
-    Actor, FoodDelivery, RowLevelChangePermissionModel)
+    Actor, FoodDelivery, RowLevelChangePermissionModel, Paper, CoverLetter)
 
 
 class AdminViewBasicTest(TestCase):
             else:
                 self.assertEqual(response.status_code, 404)
 
+    def test_add_model_modeladmin_only_qs(self):
+        # only() is used in ModelAdmin.queryset()
+        p = Paper.objects.create(title=u"My Paper Title")
+        self.assertEqual(Paper.objects.count(), 1)
+        response = self.client.get('/test_admin/admin/admin_views/paper/%s/' % p.pk)
+        self.assertEqual(response.status_code, 200)
+        post_data = {
+            "title": u"My Modified Paper Title",
+            "_save": "Save",
+        }
+        response = self.client.post('/test_admin/admin/admin_views/paper/%s/' % p.pk,
+                post_data, follow=True)
+        self.assertEqual(response.status_code, 200)
+        # Message should contain non-ugly model name. Instance representation is set by unicode() (ugly)
+        self.assertContains(response, '<li class="info">The paper &quot;Paper_Deferred_author object&quot; was changed successfully.</li>')
+
+        # defer() is used in ModelAdmin.queryset()
+        cl = CoverLetter.objects.create(author=u"John Doe")
+        self.assertEqual(CoverLetter.objects.count(), 1)
+        response = self.client.get('/test_admin/admin/admin_views/coverletter/%s/' % cl.pk)
+        self.assertEqual(response.status_code, 200)
+        post_data = {
+            "author": u"John Doe II",
+            "_save": "Save",
+        }
+        response = self.client.post('/test_admin/admin/admin_views/coverletter/%s/' % cl.pk,
+                post_data, follow=True)
+        self.assertEqual(response.status_code, 200)
+        # Message should contain non-ugly model name. Instance representation is set by model's __unicode__()
+        self.assertContains(response, '<li class="info">The cover letter &quot;John Doe II&quot; was changed successfully.</li>')
+
 class AdminInlineFileUploadTest(TestCase):
     fixtures = ['admin-views-users.xml', 'admin-views-actions.xml']
     urlbit = 'admin'
             response = self.client.get(url)
             self.assert_non_localized_year(response, 2000)
             self.assert_non_localized_year(response, 2003)
-            self.assert_non_localized_year(response, 2005)
+            self.assert_non_localized_year(response, 2005)