Reputation: 8539
I'm trying to customize the admin index of an open source app without changing too many things.
I thought I could easily do what I need just by checking the name of the models contained in app_list.models. Something like:
if model is "interface" or "device":
show it
But i can't find a value that doesn't change depending on the settings and language of the django instance.
if i print some info in the template with:
{{ app_list.1.models.2 }}
I get:
{'perms':
{'add': True, 'change': True, 'delete': True},
'add_url': '/nodeshot-server/admin/nodeshot/interface/add/',
'admin_url': '/nodeshot-server/admin/nodeshot/interface/',
'name': <django.utils.functional.__proxy__ object at 0x7f67fced6ed0>
}
The add_url and admin_url depend on the settings, infact on my local instance "nodeshot-server" is the folder of the project while online is just "/admin/nodeshot/ecc.". The name key is also unreliable because it depends on the language in use, so if the language is Italian the name key won't be "interface" but "interfaccia".
Is there a way to retrieve the original name of the model (instead of the translated one) without changing the view? I don't really know what "django.utils.functional._proxy" although it looks like a mecchanism for lazy evaluation.
If is not possible I suppose I will have to write a custom view for the admin index.. right?
Upvotes: 0
Views: 318
Reputation: 8539
The correct answer to this question is:
Use the bit that doesn't change in the if clause:
{% if '/nodeshot/interface/' in model.admin_url %}
Upvotes: 0
Reputation: 14230
You can override the admin template for each model. And don't duplicate the whole template file, override just the template block you want to change.
Upvotes: 0
Reputation: 239430
Two options here. First, yes, you could simply override AdminSite.index
and pass whatever you like to the context. If you go with that approach, you'll have to create and use an AdminSite
subclass, and register your apps manually (admin.autodiscover()
will no longer work).
The second option is to try to make use of the info you are provided. admin_url
may change in part depending on context, but the last piece /nodeshot/interface
will always be the same for that app and model. You can use a regex to search for this piece and respond accordingly. Though, since you're in the template at that point, you'd have to create a custom templatetag or filter to do the work. A filter would probably be your best bet. You can pass the model dict to it and return a boolean, which you can then use inside if
statements.
nodeshot/templatetags/nodeshot_filters.py
import re
@register.filter
def goes_in_different_table(model_dict):
if re.search(r'/nodeshot/interface/', model_dict['admin_url']):
return True
return False
templates/admin/app_index.html
{% load nodeshot_filters %}
{% if model|goes_in_different_table %}
<!-- do something -->
{% endif %}
Upvotes: 1