Ticket #9749: patch_django_9749.20090111.diff

File patch_django_9749.20090111.diff, 5.3 KB (added by david, 6 years ago)

First iteration, review welcome

  • django/contrib/admin/validation.py

     
    55
    66from django.core.exceptions import ImproperlyConfigured
    77from django.db import models
    88from django.forms.models import BaseModelForm, BaseModelFormSet, fields_for_model
    99from django.contrib.admin.options import flatten_fieldsets, BaseModelAdmin
    1010from django.contrib.admin.options import HORIZONTAL, VERTICAL
     11from django.contrib.admin.views.main import ChangeList
    1112
    1213__all__ = ['validate']
    1314
     
    138144        raise ImproperlyConfigured("'%s.formset' does not inherit from "
    139145                "BaseModelFormSet." % cls.__name__)
    140146
     147    # exclude
     148    if hasattr(cls, 'exclude') and cls.exclude:
     149        fk_name = _get_foreign_key(parent_model, cls.model).name
     150        if fk_name in cls.exclude:
     151            raise ImproperlyConfigured("'%s.exclude' should not contain "
     152                    "'%s' key because it's used in "
     153                    "%s." % (cls.__name__, fk_name, parent.__name__))
     154
    141155def validate_base(cls, model):
    142156    opts = model._meta
    143157
  • django/contrib/admin/options.py

     
    153153
    154154class ModelAdmin(BaseModelAdmin):
    155155    "Encapsulates all admin options and functionality for a given model."
     156    # Avoid circular import of ChangeList
     157    from django.contrib.admin.views.main import ChangeList
     158   
    156159    __metaclass__ = forms.MediaDefiningClass
    157160
    158161    list_display = ('__str__',)
     
    166169    save_on_top = False
    167170    ordering = None
    168171    inlines = []
     172    changelist_class = ChangeList
    169173
    170174    # Custom templates (designed to be over-ridden in subclasses)
    171175    change_form_template = None
     
    620624
    621625    def changelist_view(self, request, extra_context=None):
    622626        "The 'change list' admin view for this model."
    623         from django.contrib.admin.views.main import ChangeList, ERROR_FLAG
    624627        opts = self.model._meta
    625628        app_label = opts.app_label
    626629        if not self.has_change_permission(request, None):
    627630            raise PermissionDenied
    628631        try:
    629             cl = ChangeList(request, self.model, self.list_display, self.list_display_links, self.list_filter,
     632            cl = self.changelist_class(request, self.model, self.list_display, self.list_display_links, self.list_filter,
    630633                self.date_hierarchy, self.search_fields, self.list_select_related, self.list_per_page, self)
    631634        except IncorrectLookupParameters:
    632635            # Wacky lookup parameters were given, so redirect to the main
     
    634637            # parameter via the query string. If wacky parameters were given and
    635638            # the 'invalid=1' parameter was already in the query string, something
    636639            # is screwed up with the database, so display an error page.
     640            from django.contrib.admin.views.main import ERROR_FLAG
    637641            if ERROR_FLAG in request.GET.keys():
    638642                return render_to_response('admin/invalid_setup.html', {'title': _('Database error')})
    639643            return HttpResponseRedirect(request.path + '?' + ERROR_FLAG + '=1')
  • tests/regressiontests/modeladmin/models.py

     
    912912...     inlines = [ValidationTestInline]
    913913>>> validate(ValidationTestModelAdmin, ValidationTestModel)
    914914
     915# changelist_class
     916
     917>>> class ValidationTestModelAdmin(ModelAdmin):
     918...     changelist_class = object
     919>>> validate(ValidationTestModelAdmin, ValidationTestModel)
     920Traceback (most recent call last):
     921...
     922ImproperlyConfigured: 'ValidationTestModelAdmin.changelist_class' does not inherit from admin.views.main.ChangeList.
     923
     924>>> from django.contrib.admin.views.main import ChangeList
     925>>> class ValidationTestChangeList(ChangeList):
     926...     def url_for_result(self, result):
     927...         return "custom/%s/" % quote(getattr(result, self.pk_attname))
     928>>> class ValidationTestModelAdmin(ModelAdmin):
     929...     changelist_class = ValidationTestChangeList
     930>>> validate(ValidationTestModelAdmin, ValidationTestModel)
     931
    915932"""
    916933}
  • docs/ref/contrib/admin.txt

     
    7777    class AuthorAdmin(admin.ModelAdmin):
    7878        date_hierarchy = 'pub_date'
    7979
     80``changelist_class``
     81~~~~~~~~~~~~~~~~~~
     82
     83Set ``changelist_class`` to a class which inherits from
     84``admin.views.main.ChangeList``, and the change list page will use this class
     85to render the list.
     86
     87Example::
     88
     89    from django.contrib.admin.views.main import ChangeList
     90   
     91    class CustomChangeList(ChangeList):
     92        def url_for_result(self, result):
     93            return "custom/%s/" % quote(getattr(result, self.pk_attname))
     94   
     95    class AuthorAdmin(admin.ModelAdmin):
     96        date_hierarchy = 'pub_date'
     97        changelist_class = CustomChangeList
     98   
     99
    80100``date_hierarchy``
    81101~~~~~~~~~~~~~~~~~~
    82102
Back to Top