Ticket #13618: 13618.diff
File 13618.diff, 3.2 KB (added by , 14 years ago) |
---|
-
django/contrib/admin/helpers.py
diff -r ad6737af409f django/contrib/admin/helpers.py
a b 26 26 class AdminForm(object): 27 27 def __init__(self, form, fieldsets, prepopulated_fields, readonly_fields=None, model_admin=None): 28 28 self.form, self.fieldsets = form, normalize_fieldsets(fieldsets) 29 self.prepopulated_fields = [{30 'field': form[field_name],31 'dependencies': [form[f] for f in dependencies]32 } for field_name, dependencies in prepopulated_fields.items()]33 29 self.model_admin = model_admin 34 30 if readonly_fields is None: 35 31 readonly_fields = () 36 32 self.readonly_fields = readonly_fields 33 self.prepopulated_fields = [{ 34 'field': form[field_name], 35 'dependencies': [form[f] for f in dependencies] 36 } for field_name, dependencies in prepopulated_fields.items() 37 if field_name not in self.readonly_fields] 37 38 38 39 def __iter__(self): 39 40 for name, options in self.fieldsets: -
tests/regressiontests/admin_validation/models.py
diff -r ad6737af409f tests/regressiontests/admin_validation/models.py
a b 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
diff -r ad6737af409f tests/regressiontests/admin_validation/tests.py
a b 1 1 from django.contrib import admin 2 2 from django.contrib.admin.validation import validate 3 from django.contrib.admin import helpers 3 4 from django.test import TestCase 5 from django import forms 6 from django.http import HttpRequest 4 7 5 from models import Song 8 from models import Song, Album 6 9 7 10 8 11 class ValidationTestCase(TestCase): … … 16 19 ] 17 20 18 21 validate(SongAdmin, Song) 22 23 def test_prepopulated_and_readonly(self): 24 """ 25 Regression for ticket #13618. 26 27 In some cases, you may want a field to be prepopulated part of the 28 time, and readonly at other times. One can use the get_readonly_fields 29 method to change the readonly fields dynamically, but adding a 30 prepopulated field to the list of readonly fields generated an error: 31 32 KeyError: "Key 'slug' not found in Form" 33 """ 34 class AlbumAdmin(admin.ModelAdmin): 35 prepopulated_fields = {'slug': ['title']} 36 readonly_fields = ['slug'] 37 request = HttpRequest() 38 adm = AlbumAdmin(Album, admin.site) 39 Form = adm.get_form(request, None) 40 adm_form = helpers.AdminForm(Form(), list(adm.get_fieldsets(request)), 41 adm.prepopulated_fields, adm.get_readonly_fields(request), 42 model_admin=self) 43 self.assertEqual(adm_form.prepopulated_fields, []) 44 self.assertEqual(adm_form.readonly_fields, ['slug'])