Ticket #13618: 13618_updated.diff
File 13618_updated.diff, 3.5 KB (added by , 14 years ago) |
---|
-
django/contrib/admin/helpers.py
25 25 class AdminForm(object): 26 26 def __init__(self, form, fieldsets, prepopulated_fields, readonly_fields=None, model_admin=None): 27 27 self.form, self.fieldsets = form, normalize_fieldsets(fieldsets) 28 self.prepopulated_fields = [{29 'field': form[field_name],30 'dependencies': [form[f] for f in dependencies]31 } for field_name, dependencies in prepopulated_fields.items()]32 28 self.model_admin = model_admin 33 29 if readonly_fields is None: 34 30 readonly_fields = () 35 31 self.readonly_fields = readonly_fields 32 self.prepopulated_fields = [{ 33 'field': form[field_name], 34 'dependencies': [form[f] for f in dependencies] 35 } for field_name, dependencies in prepopulated_fields.items() 36 if field_name not in self.readonly_fields] 36 37 37 38 def __iter__(self): 38 39 for name, options in self.fieldsets: -
tests/regressiontests/admin_validation/models.py
7 7 8 8 class Album(models.Model): 9 9 title = models.CharField(max_length=150) 10 slug = models.CharField(max_length=150) 10 11 11 12 12 13 class Song(models.Model): -
tests/regressiontests/admin_validation/tests.py
1 1 from django.contrib import admin 2 2 from django import forms 3 from django.contrib.admin import helpers 3 4 from django.contrib.admin.validation import validate, validate_inline, \ 4 5 ImproperlyConfigured 5 6 from django.test import TestCase 7 from django.http import HttpRequest 6 8 7 9 from models import Song, Book, Album, TwoAlbumFKAndAnE 8 10 … … 153 155 154 156 validate(SongAdmin, Song) 155 157 158 def test_prepopulated_readonly(self): 159 """ 160 Regression for ticket #13618. 161 162 In some cases, you may want a field to be prepopulated part of the 163 time, and readonly at other times. One can use the get_readonly_fields 164 method to change the readonly fields dynamically, but adding a 165 prepopulated field to the list of readonly fields generated an error: 166 167 KeyError: "Key 'slug' not found in Form" 168 """ 169 class AlbumAdmin(admin.ModelAdmin): 170 prepopulated_fields = {'slug': ['title']} 171 172 def get_readonly_fields(self, request, obj=None): 173 return ['slug'] 174 175 request = HttpRequest() 176 adm = AlbumAdmin(Album, admin.site) 177 Form = adm.get_form(request, None) 178 adm_form = helpers.AdminForm(Form(), list(adm.get_fieldsets(request)), 179 adm.prepopulated_fields, adm.get_readonly_fields(request), 180 model_admin=self) 181 self.assertEqual(adm_form.prepopulated_fields, []) 182 self.assertEqual(adm_form.readonly_fields, ['slug']) 183 156 184 def test_nonexistant_field(self): 157 185 class SongAdmin(admin.ModelAdmin): 158 186 readonly_fields = ("title", "nonexistant")