Ticket #9749: patch_django_9749.20090318.diff
File patch_django_9749.20090318.diff, 8.3 KB (added by , 16 years ago) |
---|
-
django/contrib/admin/validation.py
5 5 6 6 from django.core.exceptions import ImproperlyConfigured 7 7 from django.db import models 8 8 from django.forms.models import BaseModelForm, BaseModelFormSet, fields_for_model 9 9 from django.contrib.admin.options import flatten_fieldsets, BaseModelAdmin 10 10 from django.contrib.admin.options import HORIZONTAL, VERTICAL 11 from django.contrib.admin.views.main import ChangeList 11 12 12 13 __all__ = ['validate'] 13 14 … … 149 150 raise ImproperlyConfigured("'%s.inlines[%d].model' does not " 150 151 "inherit from models.Model." % (cls.__name__, idx)) 151 152 validate_base(inline, inline.model) 152 validate_inline(inline )153 validate_inline(inline, cls, model) 153 154 154 def validate_inline(cls): 155 # changelist_class = ChangeList 156 if hasattr(cls, 'changelist_class') and not issubclass(cls.changelist_class, ChangeList): 157 raise ImproperlyConfigured("'%s.changelist_class' does not inherit " 158 "from admin.views.main.ChangeList." % cls.__name__) 159 160 def validate_inline(cls, parent, parent_model): 155 161 # model is already verified to exist and be a Model 156 162 if cls.fk_name: # default value is None 157 163 f = get_field(cls, cls.model, cls.model._meta, 'fk_name', cls.fk_name) -
django/contrib/admin/options.py
171 171 172 172 class ModelAdmin(BaseModelAdmin): 173 173 "Encapsulates all admin options and functionality for a given model." 174 # Avoid circular import of ChangeList 175 from django.contrib.admin.views.main import ChangeList 176 174 177 __metaclass__ = forms.MediaDefiningClass 175 178 176 179 list_display = ('__str__',) … … 185 188 save_on_top = False 186 189 ordering = None 187 190 inlines = [] 188 191 changelist_class = ChangeList 192 189 193 # Custom templates (designed to be over-ridden in subclasses) 190 194 change_form_template = None 191 195 change_list_template = None … … 703 707 704 708 def changelist_view(self, request, extra_context=None): 705 709 "The 'change list' admin view for this model." 706 from django.contrib.admin.views.main import ChangeList, ERROR_FLAG707 710 opts = self.model._meta 708 711 app_label = opts.app_label 709 712 if not self.has_change_permission(request, None): 710 713 raise PermissionDenied 711 714 try: 712 cl = ChangeList(request, self.model, self.list_display, self.list_display_links, self.list_filter,715 cl = self.changelist_class(request, self.model, self.list_display, self.list_display_links, self.list_filter, 713 716 self.date_hierarchy, self.search_fields, self.list_select_related, self.list_per_page, self.list_editable, self) 714 717 except IncorrectLookupParameters: 715 718 # Wacky lookup parameters were given, so redirect to the main … … 717 720 # parameter via the query string. If wacky parameters were given and 718 721 # the 'invalid=1' parameter was already in the query string, something 719 722 # is screwed up with the database, so display an error page. 723 from django.contrib.admin.views.main import ERROR_FLAG 720 724 if ERROR_FLAG in request.GET.keys(): 721 725 return render_to_response('admin/invalid_setup.html', {'title': _('Database error')}) 722 726 return HttpResponseRedirect(request.path + '?' + ERROR_FLAG + '=1') -
tests/regressiontests/admin_views/tests.py
875 875 self.failUnlessEqual(FooAccount.objects.all()[0].username, "%s-1" % foo_user) 876 876 self.failUnlessEqual(BarAccount.objects.all()[0].username, "%s-1" % bar_user) 877 877 self.failUnlessEqual(Persona.objects.all()[0].accounts.count(), 2) 878 879 880 class AdminCustomChangeListTest(TestCase): 881 fixtures = ['admin-views-users.xml'] 882 883 def setUp(self): 884 self.client.login(username='super', password='secret') 885 886 def tearDown(self): 887 self.client.logout() 888 889 def testCustomLinkPresence(self): 890 """ 891 A test to ensure that custom ChangeList class works as expected. 892 """ 893 post_data = { 894 "title": u"Lion", 895 } 896 response = self.client.post('/test_admin/admin/admin_views/animal/add/', post_data) 897 self.failUnlessEqual(response.status_code, 302) # redirect somewhere 898 response = self.client.get('/test_admin/admin/admin_views/animal/') 899 self.failUnlessEqual(response.status_code, 200) 900 should_contain = """<a href="custom/1/">Lion</a>""" 901 self.assertContains(response, should_contain) 902 903 -
tests/regressiontests/admin_views/models.py
165 166 def __unicode__(self): 166 167 return self.name 167 168 169 class Animal(models.Model): 170 title = models.CharField(max_length=20) 171 def __unicode__(self): 172 return self.title 173 174 class AnimalChangeList(admin.views.main.ChangeList): 175 def url_for_result(self, result): 176 return "custom/%s/" % admin.util.quote(getattr(result, self.pk_attname)) 177 168 178 class Account(models.Model): 169 179 """ 170 180 A simple, generic account encapsulating the information shared by all … … 199 209 BarAccountAdmin 200 210 ) 201 211 212 class AnimalAdmin(admin.ModelAdmin): 213 changelist_class = AnimalChangeList 202 214 215 203 216 admin.site.register(Article, ArticleAdmin) 204 217 admin.site.register(CustomArticle, CustomArticleAdmin) 205 218 admin.site.register(Section, inlines=[ArticleInline]) … … 208 221 admin.site.register(Thing, ThingAdmin) 209 222 admin.site.register(Person, PersonAdmin) 210 223 admin.site.register(Persona, PersonaAdmin) 224 admin.site.register(Animal, AnimalAdmin) 211 225 212 226 # We intentionally register Promo and ChapterXtra1 but not Chapter nor ChapterXtra2. 213 227 # That way we cover all four cases: -
tests/regressiontests/modeladmin/models.py
912 912 ... inlines = [ValidationTestInline] 913 913 >>> validate(ValidationTestModelAdmin, ValidationTestModel) 914 914 915 # changelist_class 916 917 >>> class ValidationTestModelAdmin(ModelAdmin): 918 ... changelist_class = object 919 >>> validate(ValidationTestModelAdmin, ValidationTestModel) 920 Traceback (most recent call last): 921 ... 922 ImproperlyConfigured: '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 915 932 """ 916 933 } -
docs/ref/contrib/admin.txt
77 77 class AuthorAdmin(admin.ModelAdmin): 78 78 date_hierarchy = 'pub_date' 79 79 80 ``changelist_class`` 81 ~~~~~~~~~~~~~~~~~~ 82 83 Set ``changelist_class`` to a class which inherits from 84 ``admin.views.main.ChangeList``, and the change list page will use this class 85 to render the list. 86 87 Example:: 88 89 from django.contrib import admin 90 91 class CustomChangeList(admin.views.main.ChangeList): 92 def url_for_result(self, result): 93 return "custom/%s/" % admin.util.quote(getattr(result, self.pk_attname)) 94 95 class AuthorAdmin(admin.ModelAdmin): 96 date_hierarchy = 'pub_date' 97 changelist_class = CustomChangeList 98 99 80 100 ``date_hierarchy`` 81 101 ~~~~~~~~~~~~~~~~~~ 82 102