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.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@15596 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Ramiro Morales 2011-02-20 23:09:25 +00:00
parent 674062c355
commit 52fc61e0cf
3 changed files with 75 additions and 4 deletions

View File

@ -748,9 +748,16 @@ class ModelAdmin(BaseModelAdmin):
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 "_continue" in request.POST:
self.message_user(request, msg + ' ' + _("You may edit it again below."))
if "_popup" in request.REQUEST:
@ -758,11 +765,11 @@ class ModelAdmin(BaseModelAdmin):
else:
return HttpResponseRedirect(request.path)
elif "_saveasnew" in request.POST:
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 "_addanother" in request.POST:
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)

View File

@ -704,6 +704,37 @@ class FoodDeliveryAdmin(admin.ModelAdmin):
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)
@ -743,6 +774,8 @@ admin.site.register(WorkHour, WorkHourAdmin)
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:

View File

@ -36,7 +36,7 @@ from models import (Article, BarAccount, CustomArticle, EmptyModel,
Language, Collector, Widget, Grommet, DooHickey, FancyDoodad, Whatsit,
Category, Post, Plot, FunkyTag, Chapter, Book, Promo, WorkHour, Employee,
Question, Answer, Inquisition, Actor, FoodDelivery,
RowLevelChangePermissionModel)
RowLevelChangePermissionModel, Paper, CoverLetter)
class AdminViewBasicTest(TestCase):
@ -2029,6 +2029,37 @@ class AdminCustomQuerysetTest(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'