From f88c2f16e84703cff225e162297a6bcedebe8910 Mon Sep 17 00:00:00 2001 From: Karen Tracey Date: Sun, 21 Mar 2010 14:23:25 +0000 Subject: [PATCH] Fixed #13004: Ensure the add page for a model with a ManyToManyField specified in readonly_fields does not raise an exception. Thanks hejsan, mlavin, copelco. git-svn-id: http://code.djangoproject.com/svn/django/trunk@12827 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/admin/helpers.py | 2 +- tests/regressiontests/admin_views/models.py | 12 ++++++++++++ tests/regressiontests/admin_views/tests.py | 5 +++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/django/contrib/admin/helpers.py b/django/contrib/admin/helpers.py index 3c1febd26b..b969653317 100644 --- a/django/contrib/admin/helpers.py +++ b/django/contrib/admin/helpers.py @@ -154,7 +154,7 @@ class AdminReadonlyField(object): field, obj, model_admin = self.field, self.form.instance, self.model_admin try: f, attr, value = lookup_field(field, obj, model_admin) - except (AttributeError, ObjectDoesNotExist): + except (AttributeError, ValueError, ObjectDoesNotExist): result_repr = EMPTY_CHANGELIST_VALUE else: if f is None: diff --git a/tests/regressiontests/admin_views/models.py b/tests/regressiontests/admin_views/models.py index fffc40571e..e66e65fa85 100644 --- a/tests/regressiontests/admin_views/models.py +++ b/tests/regressiontests/admin_views/models.py @@ -565,6 +565,16 @@ class CyclicTwo(models.Model): def __unicode__(self): return self.name +class Topping(models.Model): + name = models.CharField(max_length=20) + +class Pizza(models.Model): + name = models.CharField(max_length=20) + toppings = models.ManyToManyField('Topping') + +class PizzaAdmin(admin.ModelAdmin): + readonly_fields = ('toppings',) + admin.site.register(Article, ArticleAdmin) admin.site.register(CustomArticle, CustomArticleAdmin) admin.site.register(Section, save_as=True, inlines=[ArticleInline]) @@ -609,3 +619,5 @@ admin.site.register(CyclicTwo) admin.site.register(Book, inlines=[ChapterInline]) admin.site.register(Promo) admin.site.register(ChapterXtra1) +admin.site.register(Pizza, PizzaAdmin) +admin.site.register(Topping) diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py index 280a89d41e..3414ea0646 100644 --- a/tests/regressiontests/admin_views/tests.py +++ b/tests/regressiontests/admin_views/tests.py @@ -1986,6 +1986,11 @@ class ReadonlyTest(TestCase): p = Post.objects.order_by('-id')[0] self.assertEqual(p.posted, datetime.date.today()) + def test_readonly_manytomany(self): + "Regression test for #13004" + response = self.client.get('/test_admin/admin/admin_views/pizza/add/') + self.assertEqual(response.status_code, 200) + class IncompleteFormTest(TestCase): """ Tests validation of a ModelForm that doesn't explicitly have all data