Ticket #11639: pre_populated.diff

File pre_populated.diff, 9.5 KB (added by leanmeandonothingmachine, 4 years ago)
  • docs/ref/contrib/admin/index.txt

     
    831831    a ``list`` or ``tuple`` of field names that will be displayed as read-only,
    832832    as described above in the :attr:`ModelAdmin.readonly_fields` section.
    833833
     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
    834843.. method:: ModelAdmin.get_urls(self)
    835844
    836845    The ``get_urls`` method on a ``ModelAdmin`` returns the URLs to be used for
  • django/contrib/admin/options.py

     
    193193    def get_readonly_fields(self, request, obj=None):
    194194        return self.readonly_fields
    195195
     196    def get_prepopulated_fields(self, request, obj=None):
     197        """
     198        Hook for specifying customizing prepopulated fields.
     199        """
     200        return self.prepopulated_fields
     201
    196202    def queryset(self, request):
    197203        """
    198204        Returns a QuerySet of all model instances that can be edited by the
     
    898904                formsets.append(formset)
    899905
    900906        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),
    902909            model_admin=self)
    903910        media = self.media + adminForm.media
    904911
     
    906913        for inline, formset in zip(self.inline_instances, formsets):
    907914            fieldsets = list(inline.get_fieldsets(request))
    908915            readonly = list(inline.get_readonly_fields(request))
     916            prepopulated = dict(inline.get_prepopulated_fields(request))
    909917            inline_admin_formset = helpers.InlineAdminFormSet(inline, formset,
    910                 fieldsets, readonly, model_admin=self)
     918                fieldsets, prepopulated, readonly, model_admin=self)
    911919            inline_admin_formsets.append(inline_admin_formset)
    912920            media = media + inline_admin_formset.media
    913921
     
    989997                formsets.append(formset)
    990998
    991999        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),
    9931002            model_admin=self)
    9941003        media = self.media + adminForm.media
    9951004
     
    9971006        for inline, formset in zip(self.inline_instances, formsets):
    9981007            fieldsets = list(inline.get_fieldsets(request, obj))
    9991008            readonly = list(inline.get_readonly_fields(request, obj))
     1009            prepopulated = dict(inline.get_prepopulated_fields(request, obj))
    10001010            inline_admin_formset = helpers.InlineAdminFormSet(inline, formset,
    1001                 fieldsets, readonly, model_admin=self)
     1011                fieldsets, prepopulated, readonly, model_admin=self)
    10021012            inline_admin_formsets.append(inline_admin_formset)
    10031013            media = media + inline_admin_formset.media
    10041014
  • django/contrib/admin/helpers.py

     
    192192    """
    193193    A wrapper around an inline formset for use in the admin system.
    194194    """
    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):
    196197        self.opts = inline
    197198        self.formset = formset
    198199        self.fieldsets = fieldsets
     
    200201        if readonly_fields is None:
    201202            readonly_fields = ()
    202203        self.readonly_fields = readonly_fields
     204        if prepopulated_fields is None:
     205            prepopulated_fields = {}
     206        self.prepopulated_fields = prepopulated_fields
    203207
    204208    def __iter__(self):
    205209        for form, original in zip(self.formset.initial_forms, self.formset.get_queryset()):
    206210            yield InlineAdminForm(self.formset, form, self.fieldsets,
    207                 self.opts.prepopulated_fields, original, self.readonly_fields,
     211                self.prepopulated_fields, original, self.readonly_fields,
    208212                model_admin=self.model_admin)
    209213        for form in self.formset.extra_forms:
    210214            yield InlineAdminForm(self.formset, form, self.fieldsets,
    211                 self.opts.prepopulated_fields, None, self.readonly_fields,
     215                self.prepopulated_fields, None, self.readonly_fields,
    212216                model_admin=self.model_admin)
    213217        yield InlineAdminForm(self.formset, self.formset.empty_form,
    214             self.fieldsets, self.opts.prepopulated_fields, None,
     218            self.fieldsets, self.prepopulated_fields, None,
    215219            self.readonly_fields, model_admin=self.model_admin)
    216220
    217221    def fields(self):
  • tests/regressiontests/admin_views/fixtures/admin-views-users.xml

     
    8888        <field type="DateTimeField" name="date">2009-03-18 11:54:58</field>
    8989        <field to="admin_views.section" name="section" rel="ManyToOneRel">1</field>
    9090    </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

     
    23612361        self.assertEqual(get_max_age(response), None)
    23622362
    23632363
     2364class 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
    23642388class ReadonlyTest(TestCase):
    23652389    fixtures = ['admin-views-users.xml']
    23662390
  • tests/regressiontests/admin_views/models.py

     
    522522    readonly_fields = ("posted",)
    523523
    524524
     525class PrePopulatedPost(models.Model):
     526    title = models.CharField(max_length=100)
     527    published = models.BooleanField()
     528    slug = models.SlugField()
     529
     530class PrePopulatedSubPost(models.Model):
     531    post = models.ForeignKey(PrePopulatedPost)
     532    subtitle = models.CharField(max_length=100)
     533    subslug = models.SlugField()
     534
     535class 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
     550class 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
    525566class Post(models.Model):
    526567    title = models.CharField(max_length=100)
    527568    content = models.TextField()
     
    724765admin.site.register(Album, AlbumAdmin)
    725766admin.site.register(Question)
    726767admin.site.register(Answer)
     768admin.site.register(PrePopulatedPost, PrePopulatedPostAdmin)
Back to Top