Code

Ticket #13618: 13618.diff

File 13618.diff, 3.2 KB (added by tobias, 4 years ago)

slightly improved fix, with test

Line 
1diff -r ad6737af409f django/contrib/admin/helpers.py
2--- a/django/contrib/admin/helpers.py   Sat Sep 11 11:55:46 2010 -0700
3+++ b/django/contrib/admin/helpers.py   Sat Sep 11 15:45:51 2010 -0700
4@@ -26,14 +26,15 @@
5 class AdminForm(object):
6     def __init__(self, form, fieldsets, prepopulated_fields, readonly_fields=None, model_admin=None):
7         self.form, self.fieldsets = form, normalize_fieldsets(fieldsets)
8-        self.prepopulated_fields = [{
9-            'field': form[field_name],
10-            'dependencies': [form[f] for f in dependencies]
11-        } for field_name, dependencies in prepopulated_fields.items()]
12         self.model_admin = model_admin
13         if readonly_fields is None:
14             readonly_fields = ()
15         self.readonly_fields = readonly_fields
16+        self.prepopulated_fields = [{
17+            'field': form[field_name],
18+            'dependencies': [form[f] for f in dependencies]
19+        } for field_name, dependencies in prepopulated_fields.items()
20+              if field_name not in self.readonly_fields]
21 
22     def __iter__(self):
23         for name, options in self.fieldsets:
24diff -r ad6737af409f tests/regressiontests/admin_validation/models.py
25--- a/tests/regressiontests/admin_validation/models.py  Sat Sep 11 11:55:46 2010 -0700
26+++ b/tests/regressiontests/admin_validation/models.py  Sat Sep 11 15:45:51 2010 -0700
27@@ -7,6 +7,7 @@
28 
29 class Album(models.Model):
30     title = models.CharField(max_length=150)
31+    slug = models.CharField(max_length=150)
32 
33 
34 class Song(models.Model):
35diff -r ad6737af409f tests/regressiontests/admin_validation/tests.py
36--- a/tests/regressiontests/admin_validation/tests.py   Sat Sep 11 11:55:46 2010 -0700
37+++ b/tests/regressiontests/admin_validation/tests.py   Sat Sep 11 15:45:51 2010 -0700
38@@ -1,8 +1,11 @@
39 from django.contrib import admin
40 from django.contrib.admin.validation import validate
41+from django.contrib.admin import helpers
42 from django.test import TestCase
43+from django import forms
44+from django.http import HttpRequest
45 
46-from models import Song
47+from models import Song, Album
48 
49 
50 class ValidationTestCase(TestCase):
51@@ -16,3 +19,26 @@
52             ]
53         
54         validate(SongAdmin, Song)
55+
56+    def test_prepopulated_and_readonly(self):
57+        """
58+        Regression for ticket #13618.
59+       
60+        In some cases, you may want a field to be prepopulated part of the
61+        time, and readonly at other times.  One can use the get_readonly_fields
62+        method to change the readonly fields dynamically, but adding a
63+        prepopulated field to the list of readonly fields generated an error:
64+       
65+        KeyError: "Key 'slug' not found in Form"
66+        """
67+        class AlbumAdmin(admin.ModelAdmin):
68+            prepopulated_fields = {'slug': ['title']}
69+            readonly_fields = ['slug']
70+        request = HttpRequest()
71+        adm = AlbumAdmin(Album, admin.site)
72+        Form = adm.get_form(request, None)
73+        adm_form = helpers.AdminForm(Form(), list(adm.get_fieldsets(request)),
74+            adm.prepopulated_fields, adm.get_readonly_fields(request),
75+            model_admin=self)
76+        self.assertEqual(adm_form.prepopulated_fields, [])
77+        self.assertEqual(adm_form.readonly_fields, ['slug'])