Ticket #13618: 13618.diff

File 13618.diff, 3.2 KB (added by Tobias McNulty, 14 years ago)

slightly improved fix, with test

  • django/contrib/admin/helpers.py

    diff -r ad6737af409f django/contrib/admin/helpers.py
    a b  
    2626class AdminForm(object):
    2727    def __init__(self, form, fieldsets, prepopulated_fields, readonly_fields=None, model_admin=None):
    2828        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()]
    3329        self.model_admin = model_admin
    3430        if readonly_fields is None:
    3531            readonly_fields = ()
    3632        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]
    3738
    3839    def __iter__(self):
    3940        for name, options in self.fieldsets:
  • tests/regressiontests/admin_validation/models.py

    diff -r ad6737af409f tests/regressiontests/admin_validation/models.py
    a b  
    77
    88class Album(models.Model):
    99    title = models.CharField(max_length=150)
     10    slug = models.CharField(max_length=150)
    1011
    1112
    1213class Song(models.Model):
  • tests/regressiontests/admin_validation/tests.py

    diff -r ad6737af409f tests/regressiontests/admin_validation/tests.py
    a b  
    11from django.contrib import admin
    22from django.contrib.admin.validation import validate
     3from django.contrib.admin import helpers
    34from django.test import TestCase
     5from django import forms
     6from django.http import HttpRequest
    47
    5 from models import Song
     8from models import Song, Album
    69
    710
    811class ValidationTestCase(TestCase):
     
    1619            ]
    1720       
    1821        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'])
Back to Top