customizing django admin ChangeForm template / adding custom content

后端 未结 2 1534
难免孤独
难免孤独 2021-02-02 00:04

I\'m able to insert (lame) static text onto the change form admin page, but I\'d really like it to use the context of the current object being edited!

For instance, I w

2条回答
  •  日久生厌
    2021-02-02 00:33

    Add your extra context in change_view

    class MyObjectAdmin(admin.ModelAdmin):
    
    # A template for a very customized change view:
    change_form_template = 'admin/my_change_form.html'
    
    def get_dynamic_info(self):
        # ...
        pass
    
    def change_view(self, request, object_id, form_url='', extra_context=None):
        extra_context = extra_context or {}
        extra_context['osm_data'] = self.get_dynamic_info()
        return super(MyObjectAdmin, self).change_view(
            request, object_id, form_url, extra_context=extra_context,
        )
    

提交回复
热议问题