Ticket #9749: patch_django_9749.20090111.2.diff
File patch_django_9749.20090111.2.diff, 7.9 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 … … 117 118 raise ImproperlyConfigured("'%s.inlines[%d].model' does not " 118 119 "inherit from models.Model." % (cls.__name__, idx)) 119 120 validate_base(inline, inline.model) 120 121 validate_inline(inline) 121 122 123 # changelist_class = ChangeList 124 if hasattr(cls, 'changelist_class') and not issubclass(cls.changelist_class, ChangeList): 125 raise ImproperlyConfigured("'%s.changelist_class' does not inherit " 126 "from admin.views.main.ChangeList." % cls.__name__) 127 122 128 123 129 def validate_inline(cls): 124 130 # model is already verified to exist and be a Model 125 131 if cls.fk_name: # default value is None -
django/contrib/admin/options.py
f = get_field(cls, cls.model, cls.model._meta, 'fk_name', cls.fk_name)
153 153 154 154 class ModelAdmin(BaseModelAdmin): 155 155 "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 156 159 __metaclass__ = forms.MediaDefiningClass 157 160 158 161 list_display = ('__str__',) … … 166 169 save_on_top = False 167 170 ordering = None 168 171 inlines = [] 172 changelist_class = ChangeList 169 173 170 174 # Custom templates (designed to be over-ridden in subclasses) 171 175 change_form_template = None … … 620 624 621 625 def changelist_view(self, request, extra_context=None): 622 626 "The 'change list' admin view for this model." 623 from django.contrib.admin.views.main import ChangeList, ERROR_FLAG624 627 opts = self.model._meta 625 628 app_label = opts.app_label 626 629 if not self.has_change_permission(request, None): 627 630 raise PermissionDenied 628 631 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, 630 633 self.date_hierarchy, self.search_fields, self.list_select_related, self.list_per_page, self) 631 634 except IncorrectLookupParameters: 632 635 # Wacky lookup parameters were given, so redirect to the main … … 634 637 # parameter via the query string. If wacky parameters were given and 635 638 # the 'invalid=1' parameter was already in the query string, something 636 639 # is screwed up with the database, so display an error page. 640 from django.contrib.admin.views.main import ERROR_FLAG 637 641 if ERROR_FLAG in request.GET.keys(): 638 642 return render_to_response('admin/invalid_setup.html', {'title': _('Database error')}) 639 643 return HttpResponseRedirect(request.path + '?' + ERROR_FLAG + '=1') -
tests/regressiontests/admin_views/tests.py
735 735 self.failUnlessEqual(response.status_code, 200) 736 736 response = self.client.post('/test_admin/admin/admin_views/book/1/delete/', delete_dict) 737 737 self.assertRedirects(response, '/test_admin/admin/admin_views/book/') 738 739 class AdminCustomChangeListTest(TestCase): 740 fixtures = ['admin-views-users.xml'] 741 742 def setUp(self): 743 self.client.login(username='super', password='secret') 744 745 def tearDown(self): 746 self.client.logout() 747 748 def testCustomLinkPresence(self): 749 """ 750 A test to ensure that custom ChangeList class works as expected. 751 """ 752 post_data = { 753 "title": u"Lion", 754 } 755 response = self.client.post('/test_admin/admin/admin_views/animal/add/', post_data) 756 self.failUnlessEqual(response.status_code, 302) # redirect somewhere 757 response = self.client.get('/test_admin/admin/admin_views/animal/') 758 self.failUnlessEqual(response.status_code, 200) 759 should_contain = """<a href="custom/1/">Lion</a>""" 760 self.assertContains(response, should_contain) -
tests/regressiontests/admin_views/models.py
134 134 class ThingAdmin(admin.ModelAdmin): 135 135 list_filter = ('color',) 136 136 137 class Animal(models.Model): 138 title = models.CharField(max_length=20) 139 def __unicode__(self): 140 return self.title 141 142 class AnimalChangeList(admin.views.main.ChangeList): 143 def url_for_result(self, result): 144 return "custom/%s/" % admin.util.quote(getattr(result, self.pk_attname)) 145 146 class AnimalAdmin(admin.ModelAdmin): 147 changelist_class = AnimalChangeList 148 137 149 admin.site.register(Article, ArticleAdmin) 138 150 admin.site.register(CustomArticle, CustomArticleAdmin) 139 151 admin.site.register(Section, inlines=[ArticleInline]) 140 152 admin.site.register(ModelWithStringPrimaryKey) 141 153 admin.site.register(Color) 142 154 admin.site.register(Thing, ThingAdmin) 155 admin.site.register(Animal, AnimalAdmin) 143 156 144 157 # We intentionally register Promo and ChapterXtra1 but not Chapter nor ChapterXtra2. 145 158 # 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