Code

Ticket #13618: 13618_updated.diff

File 13618_updated.diff, 3.5 KB (added by tonnzor, 3 years ago)

Fixed tobias patch to be applicable on trunk

Line 
1Index: django/contrib/admin/helpers.py
2===================================================================
3--- django/contrib/admin/helpers.py (revision 15387)
4+++ django/contrib/admin/helpers.py (working copy)
5@@ -25,14 +25,15 @@
6 class AdminForm(object):
7     def __init__(self, form, fieldsets, prepopulated_fields, readonly_fields=None, model_admin=None):
8         self.form, self.fieldsets = form, normalize_fieldsets(fieldsets)
9-        self.prepopulated_fields = [{
10-            'field': form[field_name],
11-            'dependencies': [form[f] for f in dependencies]
12-        } for field_name, dependencies in prepopulated_fields.items()]
13         self.model_admin = model_admin
14         if readonly_fields is None:
15             readonly_fields = ()
16         self.readonly_fields = readonly_fields
17+        self.prepopulated_fields = [{
18+            'field': form[field_name],
19+            'dependencies': [form[f] for f in dependencies]
20+        } for field_name, dependencies in prepopulated_fields.items()
21+              if field_name not in self.readonly_fields]
22 
23     def __iter__(self):
24         for name, options in self.fieldsets:
25Index: tests/regressiontests/admin_validation/models.py
26===================================================================
27--- tests/regressiontests/admin_validation/models.py    (revision 15387)
28+++ tests/regressiontests/admin_validation/models.py    (working copy)
29@@ -7,6 +7,7 @@
30 
31 class Album(models.Model):
32     title = models.CharField(max_length=150)
33+    slug = models.CharField(max_length=150)
34 
35 
36 class Song(models.Model):
37Index: tests/regressiontests/admin_validation/tests.py
38===================================================================
39--- tests/regressiontests/admin_validation/tests.py (revision 15387)
40+++ tests/regressiontests/admin_validation/tests.py (working copy)
41@@ -1,8 +1,10 @@
42 from django.contrib import admin
43 from django import forms
44+from django.contrib.admin import helpers
45 from django.contrib.admin.validation import validate, validate_inline, \
46                                             ImproperlyConfigured
47 from django.test import TestCase
48+from django.http import HttpRequest
49 
50 from models import Song, Book, Album, TwoAlbumFKAndAnE
51 
52@@ -153,6 +155,32 @@
53 
54         validate(SongAdmin, Song)
55 
56+    def test_prepopulated_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+
70+            def get_readonly_fields(self, request, obj=None):
71+                return ['slug']
72+
73+        request = HttpRequest()
74+        adm = AlbumAdmin(Album, admin.site)
75+        Form = adm.get_form(request, None)
76+        adm_form = helpers.AdminForm(Form(), list(adm.get_fieldsets(request)),
77+            adm.prepopulated_fields, adm.get_readonly_fields(request),
78+            model_admin=self)
79+        self.assertEqual(adm_form.prepopulated_fields, [])
80+        self.assertEqual(adm_form.readonly_fields, ['slug'])
81+
82     def test_nonexistant_field(self):
83         class SongAdmin(admin.ModelAdmin):
84             readonly_fields = ("title", "nonexistant")