When using ModelAdmin, if you wish to add a new view alongside inspect/create/edit/delete, you need to do the following:
-
Inherit from ButtonHelper and add a new function for your view link button, which follows the same template as cancel_button()/inspect_button() etc..
-
Override get_buttons_for_obj to add your custom button...which basically involves reimplementing it exactly the same then tagging your button on to the end of the btns array. (we can call super and tag it on the end of the returned array, although this still involve repeating some of the logic within the base function)
-
In your custom ModelAdmin child class, override get_admin_urls_for_registration to add the url for your new view onto the return urls array.
-
In the same class, implement a new function to return your view (i.e. it should do the same as ModelAdmin.inspect_view but return your own view class).
-
Implement the actual View class by e.g. inheriting from InspectView
This is quite a lot of boilerplate. Similarly if you wish to remove a view/button (e.g. preventing models from being deleted), you have to reimplement a function to do exactly the same thing but without the button you wish to remove.
I wonder if there is way to easily, dynamically register views/buttons in ModelAdmin?
I am not sure how this could work yet, but perhaps a class attribute which will take an array of tuples with the view class and button name? We would then need the necessary logic to register these views.
Allowing new views to be registered like this would not only make it more generic, but allow a lot of the repeated boilerplate within the base classes to be condensed (e.g. functions like index_view and create_view on ModelAdmin, or create_button, index_button etc on ButtonHelper which share much of the same logic)
I will happily do the work to this effect, but I think it needs some discussion and consensus before beginning.
Another interesting possibility would be to allow buttons to be registered which are not attached to a view ..this could be achieved by simply setting the url parameter to '', but it would be useful to be able to specify the html id of the created <a> element.
(e.g. to allow javascript actions etc...)
When using
ModelAdmin, if you wish to add a new view alongside inspect/create/edit/delete, you need to do the following:Inherit from
ButtonHelperand add a new function for your view link button, which follows the same template ascancel_button()/inspect_button()etc..Override
get_buttons_for_objto add your custom button...which basically involves reimplementing it exactly the same then tagging your button on to the end of thebtnsarray. (we can callsuperand tag it on the end of the returned array, although this still involve repeating some of the logic within the base function)In your custom
ModelAdminchild class, overrideget_admin_urls_for_registrationto add the url for your new view onto the returnurlsarray.In the same class, implement a new function to return your view (i.e. it should do the same as
ModelAdmin.inspect_viewbut return your own view class).Implement the actual
Viewclass by e.g. inheriting fromInspectViewThis is quite a lot of boilerplate. Similarly if you wish to remove a view/button (e.g. preventing models from being deleted), you have to reimplement a function to do exactly the same thing but without the button you wish to remove.
I wonder if there is way to easily, dynamically register views/buttons in
ModelAdmin?I am not sure how this could work yet, but perhaps a class attribute which will take an array of tuples with the view class and button name? We would then need the necessary logic to register these views.
Allowing new views to be registered like this would not only make it more generic, but allow a lot of the repeated boilerplate within the base classes to be condensed (e.g. functions like
index_viewandcreate_viewonModelAdmin, orcreate_button,index_buttonetc onButtonHelperwhich share much of the same logic)I will happily do the work to this effect, but I think it needs some discussion and consensus before beginning.
Another interesting possibility would be to allow buttons to be registered which are not attached to a view ..this could be achieved by simply setting the
urlparameter to'', but it would be useful to be able to specify the htmlidof the created<a>element.(e.g. to allow javascript actions etc...)