Override admin fieldset for model in app - django

Django admin docs state that you can override templates for specific models in specific apps by putting template in my_app/my_model subdirectory of templates/admin folder in your application.
I'm trying to override includes/fieldset.html template for my_model in my_app. Saving it as my_app/admin/my_app/my_model/includes/fieldset.html doesn't seem to work, while my_app/admin/includes/fieldset.html does, but overrides template for all pages in admin site.
Is there any way to achieve this behaviour?

Yes, the behavior is correct because not every template is getting override using this method per app or model, please read this.
allowed templates are :
app_index.html
change_form.html
change_list.html
delete_confirmation.html
object_history.html
popup_response.html
So what I had done is override all these per app and model or added in admin.py class change_form_template = 'admin/appname/customtemplate.html' this will give you full control over templates. You can / need also to change all inner templates using inner includes.

Related

Use the modified admin template only for one Image app

Django 3.0.6
For one model I need a modified admin site template.
Namely, I want to modify this template:
admin/includes/fieldset.html
I have copied the fieldset.html from Django package directory and placed it like this:
/my_project/image/templates/admin/includes/fieldset.html
Here image is my application. It is this application that needs a modified admin template.
The problem is that all other models also get this template. And the used template filters don't receive necessary params and explode.
Documentation: https://docs.djangoproject.com/en/3.0/ref/contrib/admin/#set-up-your-projects-admin-template-directories
Well, I got confused and fail to organize the necessary directories structure.
How can I use the modified template only for one Image app?
You have a couple of issues. Firstly, only the following templates can be overridden per-app or per-model:
actions.html
app_index.html
change_form.html
change_form_object_tools.html
change_list.html
change_list_object_tools.html
change_list_results.html
date_hierarchy.html
delete_confirmation.html
object_history.html
pagination.html
popup_response.html
prepopulated_fields_js.html
search_form.html
submit_line.html
fieldset.html isn't in there, so you'll need to see if the including template(s) is in this list, and if it is, replace this template and use it to include your own version of fieldset.html.
Also, your location (slightly modified since can't override included templates per-app): /my_project/image/templates/admin/template.html isn't quite right. This will overwrite that template for every app. To do it per-app, you need a further subdirectory under admin:
/my_project/image/templates/admin/image/template.html
The reason for this is that templates don't really care about which app they're in, so the fact that the template lives in your image app doesn't mean anything to Django, the convention of putting them in your app's sub-directory is solely to avoid overriding templates that you don't intend to.

Hide model from main admin list, but allow creation in inline editor

In my Django app, I have an Attribute model which has a many-to-many relationship to a MeasurementMethod model.
I put an inline for MeasurementMethod in the admin interface for Attribute, but I don't think it is useful to have a separate interface for managing MeasurementMethods at all; there's no reason why a user would say, "Gee, I wonder what Attributes can be measured by water displacement."
However, this left no way to create new MeasurementMethods from the inline editor until I found Anton Belonovich's post, which says that I need to admin.site.register(MeasurementMethod) first. I did that, and sure enough the edit and create buttons appeared.
But now on the admin page, where there's a list of apps and the models that can be managed, there's an entry for MeasurementMethod that I don't want.
Is there a way to get rid of it? Or is there a better way to accomplish this?
The solution is to register the MeasurementMethod class with a custom admin class that overrides has_module_permission:
#admin.register(MeasurementMethod)
class MeasurementMethodAdmin(admin.ModelAdmin):
def has_module_permission(self, request):
return False
Then the class can still be edited inline.
ModelAdmin.has_module_permission(request)
Should return True if displaying the module on the admin index page and accessing the module’s index page is permitted, False otherwise. ... Overriding it does not restrict access to the add, change or delete views ...
You could create a custom admin site docs and then override the index method/view. Make sure you register your models with this new admin site and hook it up in the urls.py file.

Django admin - Custom change_form.html and 3rd party apps custom change_form

I am currently customizing the admin templates of my models and I ran into an issue with ModelAdmin that inherits of 3rd party ones like django-importexport or django-modelclone.
Both of these apps come either with their change_list.html or change_form.html. Each of them extends either admin/change_list.html or admin/change_form.html.
So my Model admin looks like this :
class MyModelAdmin(SuitObjectActionsMixin, ImportExportModelAdmin, ClonableModelAdmin):
I tried to customise my ModelAdmin with adding the file change_form.html into /templates/myapp/mymodel/. I know that should work as it does for another admin that doesn't inherit from any 3rd party apps.
Using the debug toolbar, it appears that the template used is the one of django-modelclone that extends the standard change_form.html and not the one I created.
It seems that 3rd party apps templates override any "local" custom template just by extending the standard one.
Is there a clean way to solve this problem ?
You can specify change form template and change list template in your ModelAdmin class:
class MyModelAdmin(A, B, C):
change_form_template = 'path/to/my_change_form.html'
change_list_template = 'path/to/my_change_list.html'
That one worked for me in similar situation.

Django-admin with callable functions

Django-admin view allows only for changing the values of model objects. Is it, however, possible to configure or change in an easy way the admin view so that it starts exposing functions on objects? I'm not talking about the functions that can be introduced in the drop-down menu on top of the object list. What I mean is a direct access to functions on model objects?
You can add your own view to the admin site by adding an "^admin/..." url in your url conf. You can use this to extend the admin site relatively easy and expose model methods through your own view. See Creating Custom Admin Views here: http://www.djangobook.com/en/1.0/chapter17/ (and another approach and notes here: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#adding-views-to-admin-sites ).
To add this as a button in the model's "change form" in the admin site, override the change_form.html template for the required models (see https://docs.djangoproject.com/en/dev/ref/contrib/admin/#overriding-admin-templates ).
You will probably want to override the object-tools block, which holds the buttons in the top right side of the page. (In Django 1.3 you can extend the object-tools-items block instead, see: https://code.djangoproject.com/ticket/12694 )
(I am quite sure one can build a nice plugin/app that automatically adds object-tools to a model from a custom "object_tools" property in the ModelAdmin with a list of model methods. Let me know id you find something like this.)

How do I use the change_form.html template in django?

I have my models and I would like to make use of the Django change_form template to edit my data.
Currently I have created my own template that works fine but lacks some of the basic stuff that change_form template might have, like fields validation.
Please give examples showing how should I call the template from my view, and what object variables need to be sent to the template.
Pointers to external projects/links that make use of this templates will be highly appreciated.
The only change_form.html template I know of is in Django's admin application. You don't use the template directly, you'll want to set up the admin application for editing your data. In order to do that, follow the steps in the documentation. If you have problems, post those problems as separate, specific questions.