Ticket #13618: 13618_updated.diff

File 13618_updated.diff, 3.5 KB (added by Artem Skoretskiy, 14 years ago)

Fixed tobias patch to be applicable on trunk

  • django/contrib/admin/helpers.py

     
    2525class AdminForm(object):
    2626    def __init__(self, form, fieldsets, prepopulated_fields, readonly_fields=None, model_admin=None):
    2727        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()]
    3228        self.model_admin = model_admin
    3329        if readonly_fields is None:
    3430            readonly_fields = ()
    3531        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]
    3637
    3738    def __iter__(self):
    3839        for name, options in self.fieldsets:
  • tests/regressiontests/admin_validation/models.py

     
    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

     
    11from django.contrib import admin
    22from django import forms
     3from django.contrib.admin import helpers
    34from django.contrib.admin.validation import validate, validate_inline, \
    45                                            ImproperlyConfigured
    56from django.test import TestCase
     7from django.http import HttpRequest
    68
    79from models import Song, Book, Album, TwoAlbumFKAndAnE
    810
     
    153155
    154156        validate(SongAdmin, Song)
    155157
     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
    156184    def test_nonexistant_field(self):
    157185        class SongAdmin(admin.ModelAdmin):
    158186            readonly_fields = ("title", "nonexistant")
Back to Top