Ticket #11639: pre_populated.diff
File pre_populated.diff, 9.5 KB (added by , 14 years ago) |
---|
-
docs/ref/contrib/admin/index.txt
831 831 a ``list`` or ``tuple`` of field names that will be displayed as read-only, 832 832 as described above in the :attr:`ModelAdmin.readonly_fields` section. 833 833 834 .. method:: ModelAdmin.get_prepopulated_fields(self, request, obj=None) 835 836 .. versionadded:: 1.3 837 838 The ``get_prepopulated_fields`` method is given the ``HttpRequest`` and the 839 ``obj`` being edited (or ``None`` on an add form) and is expected to return 840 a ``dictionary``, as described above in the :attr:`ModelAdmin.prepopulated_fields` 841 section. 842 834 843 .. method:: ModelAdmin.get_urls(self) 835 844 836 845 The ``get_urls`` method on a ``ModelAdmin`` returns the URLs to be used for -
django/contrib/admin/options.py
193 193 def get_readonly_fields(self, request, obj=None): 194 194 return self.readonly_fields 195 195 196 def get_prepopulated_fields(self, request, obj=None): 197 """ 198 Hook for specifying customizing prepopulated fields. 199 """ 200 return self.prepopulated_fields 201 196 202 def queryset(self, request): 197 203 """ 198 204 Returns a QuerySet of all model instances that can be edited by the … … 898 904 formsets.append(formset) 899 905 900 906 adminForm = helpers.AdminForm(form, list(self.get_fieldsets(request)), 901 self.prepopulated_fields, self.get_readonly_fields(request), 907 self.get_prepopulated_fields(request), 908 self.get_readonly_fields(request), 902 909 model_admin=self) 903 910 media = self.media + adminForm.media 904 911 … … 906 913 for inline, formset in zip(self.inline_instances, formsets): 907 914 fieldsets = list(inline.get_fieldsets(request)) 908 915 readonly = list(inline.get_readonly_fields(request)) 916 prepopulated = dict(inline.get_prepopulated_fields(request)) 909 917 inline_admin_formset = helpers.InlineAdminFormSet(inline, formset, 910 fieldsets, readonly, model_admin=self)918 fieldsets, prepopulated, readonly, model_admin=self) 911 919 inline_admin_formsets.append(inline_admin_formset) 912 920 media = media + inline_admin_formset.media 913 921 … … 989 997 formsets.append(formset) 990 998 991 999 adminForm = helpers.AdminForm(form, self.get_fieldsets(request, obj), 992 self.prepopulated_fields, self.get_readonly_fields(request, obj), 1000 self.get_prepopulated_fields(request, obj), 1001 self.get_readonly_fields(request, obj), 993 1002 model_admin=self) 994 1003 media = self.media + adminForm.media 995 1004 … … 997 1006 for inline, formset in zip(self.inline_instances, formsets): 998 1007 fieldsets = list(inline.get_fieldsets(request, obj)) 999 1008 readonly = list(inline.get_readonly_fields(request, obj)) 1009 prepopulated = dict(inline.get_prepopulated_fields(request, obj)) 1000 1010 inline_admin_formset = helpers.InlineAdminFormSet(inline, formset, 1001 fieldsets, readonly, model_admin=self)1011 fieldsets, prepopulated, readonly, model_admin=self) 1002 1012 inline_admin_formsets.append(inline_admin_formset) 1003 1013 media = media + inline_admin_formset.media 1004 1014 -
django/contrib/admin/helpers.py
192 192 """ 193 193 A wrapper around an inline formset for use in the admin system. 194 194 """ 195 def __init__(self, inline, formset, fieldsets, readonly_fields=None, model_admin=None): 195 def __init__(self, inline, formset, fieldsets, prepopulated_fields=None, 196 readonly_fields=None, model_admin=None): 196 197 self.opts = inline 197 198 self.formset = formset 198 199 self.fieldsets = fieldsets … … 200 201 if readonly_fields is None: 201 202 readonly_fields = () 202 203 self.readonly_fields = readonly_fields 204 if prepopulated_fields is None: 205 prepopulated_fields = {} 206 self.prepopulated_fields = prepopulated_fields 203 207 204 208 def __iter__(self): 205 209 for form, original in zip(self.formset.initial_forms, self.formset.get_queryset()): 206 210 yield InlineAdminForm(self.formset, form, self.fieldsets, 207 self. opts.prepopulated_fields, original, self.readonly_fields,211 self.prepopulated_fields, original, self.readonly_fields, 208 212 model_admin=self.model_admin) 209 213 for form in self.formset.extra_forms: 210 214 yield InlineAdminForm(self.formset, form, self.fieldsets, 211 self. opts.prepopulated_fields, None, self.readonly_fields,215 self.prepopulated_fields, None, self.readonly_fields, 212 216 model_admin=self.model_admin) 213 217 yield InlineAdminForm(self.formset, self.formset.empty_form, 214 self.fieldsets, self. opts.prepopulated_fields, None,218 self.fieldsets, self.prepopulated_fields, None, 215 219 self.readonly_fields, model_admin=self.model_admin) 216 220 217 221 def fields(self): -
tests/regressiontests/admin_views/fixtures/admin-views-users.xml
88 88 <field type="DateTimeField" name="date">2009-03-18 11:54:58</field> 89 89 <field to="admin_views.section" name="section" rel="ManyToOneRel">1</field> 90 90 </object> 91 92 93 </django-objects> 94 No newline at end of file 91 <object pk="1" model="admin_views.prepopulatedpost"> 92 <field type="TextField" name="title">A Long Title</field> 93 <field type="BooleanField" name="published">True</field> 94 <field type="SlugField" name="slug">a-long-title</field> 95 </object> 96 </django-objects> -
tests/regressiontests/admin_views/tests.py
2361 2361 self.assertEqual(get_max_age(response), None) 2362 2362 2363 2363 2364 class PrePopulatedTest(TestCase): 2365 fixtures = ['admin-views-users.xml'] 2366 2367 def setUp(self): 2368 self.client.login(username='super', password='secret') 2369 2370 def tearDown(self): 2371 self.client.logout() 2372 2373 def test_prepopulated_on(self): 2374 response = self.client.get('/test_admin/admin/admin_views/prepopulatedpost/add/') 2375 self.assertEqual(response.status_code, 200) 2376 self.assertContains(response, "id: '#id_slug',") 2377 self.assertContains(response, "field['dependency_ids'].push('#id_title');") 2378 self.assertContains(response, "id: '#id_prepopulatedsubpost_set-0-subslug',") 2379 2380 def test_prepopulated_off(self): 2381 response = self.client.get('/test_admin/admin/admin_views/prepopulatedpost/1/') 2382 self.assertEqual(response.status_code, 200) 2383 self.assertContains(response, "A Long Title") 2384 self.assertNotContains(response, "id: '#id_slug'") 2385 self.assertNotContains(response, "field['dependency_ids'].push('#id_title');") 2386 self.assertNotContains(response, "id: '#id_prepopulatedsubpost_set-0-subslug',") 2387 2364 2388 class ReadonlyTest(TestCase): 2365 2389 fixtures = ['admin-views-users.xml'] 2366 2390 -
tests/regressiontests/admin_views/models.py
522 522 readonly_fields = ("posted",) 523 523 524 524 525 class PrePopulatedPost(models.Model): 526 title = models.CharField(max_length=100) 527 published = models.BooleanField() 528 slug = models.SlugField() 529 530 class PrePopulatedSubPost(models.Model): 531 post = models.ForeignKey(PrePopulatedPost) 532 subtitle = models.CharField(max_length=100) 533 subslug = models.SlugField() 534 535 class SubPostInline(admin.TabularInline): 536 model = PrePopulatedSubPost 537 538 prepopulated_fields = { 'subslug' : ('subtitle',) } 539 540 def get_readonly_fields(self, request, obj=None): 541 if obj and obj.published: 542 return ('subslug',) 543 return self.readonly_fields 544 545 def get_prepopulated_fields(self, request, obj=None): 546 if obj and obj.published: 547 return {} 548 return self.prepopulated_fields 549 550 class PrePopulatedPostAdmin(admin.ModelAdmin): 551 list_display = ['title', 'slug'] 552 prepopulated_fields = { 'slug' : ('title',) } 553 554 inlines = [ SubPostInline ] 555 556 def get_readonly_fields(self, request, obj=None): 557 if obj and obj.published: 558 return ('slug',) 559 return self.readonly_fields 560 561 def get_prepopulated_fields(self, request, obj=None): 562 if obj and obj.published: 563 return {} 564 return self.prepopulated_fields 565 525 566 class Post(models.Model): 526 567 title = models.CharField(max_length=100) 527 568 content = models.TextField() … … 724 765 admin.site.register(Album, AlbumAdmin) 725 766 admin.site.register(Question) 726 767 admin.site.register(Answer) 768 admin.site.register(PrePopulatedPost, PrePopulatedPostAdmin)