Ticket #9932: patch_django_9932.20081230.diff

File patch_django_9932.20081230.diff, 3.0 KB (added by David Larlet, 16 years ago)

Patch with tests against r9690

  • django_src/django/contrib/admin/validation.py

     
    55
    66from django.core.exceptions import ImproperlyConfigured
    77from django.db import models
    8 from django.forms.models import BaseModelForm, BaseModelFormSet, fields_for_model
     8from django.forms.models import BaseModelForm, BaseModelFormSet, fields_for_model, _get_foreign_key
    99from django.contrib.admin.options import flatten_fieldsets, BaseModelAdmin
    1010from django.contrib.admin.options import HORIZONTAL, VERTICAL
    1111
     
    117117                raise ImproperlyConfigured("'%s.inlines[%d].model' does not "
    118118                        "inherit from models.Model." % (cls.__name__, idx))
    119119            validate_base(inline, inline.model)
    120             validate_inline(inline)
     120            validate_inline(inline, cls, model)
    121121
    122 def validate_inline(cls):
     122def validate_inline(cls, parent, parent_model):
    123123    # model is already verified to exist and be a Model
    124124    if cls.fk_name: # default value is None
    125125        f = get_field(cls, cls.model, cls.model._meta, 'fk_name', cls.fk_name)
     
    138138        raise ImproperlyConfigured("'%s.formset' does not inherit from "
    139139                "BaseModelFormSet." % cls.__name__)
    140140
     141    # exclude
     142    if hasattr(cls, 'exclude') and cls.exclude:
     143        fk_name = _get_foreign_key(parent_model, cls.model).name
     144        if fk_name in cls.exclude:
     145            raise ImproperlyConfigured("'%s.exclude' should not contain "
     146                    "'%s' key because it's used in "
     147                    "%s." % (cls.__name__, fk_name, parent.__name__))
     148
    141149def validate_base(cls, model):
    142150    opts = model._meta
    143151
  • django_src/tests/regressiontests/admin_validation/models.py

     
    44
    55from django.db import models
    66
     7class Album(models.Model):
     8    title = models.CharField(max_length=150)
     9
    710class Song(models.Model):
    811    title = models.CharField(max_length=150)
     12    first_album = models.ForeignKey(Album)
    913   
    1014    class Meta:
    1115        ordering = ('title',)
     
    4044    ...
    4145ImproperlyConfigured: 'InvalidFields.fields' refers to field 'spam' that is missing from the form.
    4246
     47#
     48# exclude in InlineModelAdmin should not contain a ForeignKey field used in
     49# ModelAdmin.model
     50#
     51
     52>>> class SongInline(admin.StackedInline):
     53...     model = Song
     54...     exclude = ['first_album']
     55
     56>>> class AlbumAdmin(admin.ModelAdmin):
     57...     model = Album
     58...     inlines = [SongInline]
     59
     60>>> validate(AlbumAdmin, Album)
     61Traceback (most recent call last):
     62    ...
     63ImproperlyConfigured: 'SongInline.exclude' should not contain 'first_album' key because it's used in AlbumAdmin.
     64
    4365"""}
Back to Top