如何在Django Admin中将模型重新分配给其他应用程序以仅用于显示 [英] How to reassign a model to a different app for display only purposes in Django Admin
问题描述
我知道我可以使用
class Meta:
verbose_name更改Django Admin中模型的显示标题。 ='此处为自定义模型名称'
但是,有没有一种方法可以显示哪个应用程序是模型的标题显示在下面?
例如,如果我在也称为 Users
>用户,则默认用户模型来自身份验证和授权>用户
到用户>用户
。
我想将其保留在原始标题身份验证和授权>用户
。
我已阅读
没有简单的方法可以完成您想要的工作,请查看生成视图的Django代码/模板:
@never_cache
def index(self,request,extra_context = None):
显示主管理员索引页面,其中列出了所有
app_list = self.get_app_list(request)
context = {
** self.each_context(request),
'title':self.index_title,
'app_list':app_list,
**(extra_context或{}),
}
request.current_app = self.name
return TemplateResponse(request,self.index_template或'admin / in dex.html',上下文)
templates / admin / index.html
:
{%for app_list%}中的应用程序
< div class = app -{{app.app_label}}模块>
< table>
< caption>
< a href = {{app.app_url}} class = section title = {%blocktrans with name = app.name%} {{name}}应用程序中的模型{%endblocktrans% }> {{app.name}}< / a>
< / caption>
{app.models中模型的%%}
< tr class = model-{{model.object_name | lower}}>
{%if model.admin_url%}
< th scope = row>< a href = {{model.admin_url}}> {{model.name}}< ; / a< / th>
{%else%}
< th scope = row> {{model.name}}< / th>
{%endif%}
{%if model.add_url%}
< td>< a href = {{model.add_url}} class = addlink> {%trans'Add'%}< / a< / td>
{%else%}
< / td>
{%endif%}
{%如果model.admin_url%}
{%如果model.view_only%}
< td>< a href = {{model.admin_url}} class = viewlink> {%trans'View'%}< / a>< / td>
{%else%}
< td>< a href = {{model.admin_url}} class = changelink> {%trans'Change'%}< / a> ;< / td>
{%endif%}
{%else%}
< td>& nbsp;< / td>
{%endif%}
< / tr>
{%endfor%}
< / table>
< / div>
{%endfor%}
如您所见,Django admin模板正在循环您的app_list直接,因此唯一的方法就是覆盖 admin / index.html
模板,以将模型放置在所需的顺序中。
从Django文档中:
对于无法以这种方式覆盖的模板,您仍然可以覆盖它们您的整个项目。只需将新版本放在您的template / admin目录中即可。这对于创建自定义404页和500页特别有用。
https://docs.djangoproject.com/en/2.2/ref/contrib/admin/ #overriding-vs-replacing-an-admin-template
I know I can change the display title for a model in Django Admin using
class Meta:
verbose_name='Custom Model Name Here'
However, is there a way to display which app heading a model is displayed under?
For example, if I create a custom user model Users
in a new app also called users
then the default user model goes from Authentication and Authorization > Users
to Users > Users
.
I would like to retain it under the original heading Authentication and Authorization > Users
.
I have read this answer which suggests changes the app verbose_name
, however it only changes the verbose name of the app associated with the model. I want to show the model in a different group on the admin panel. You can see the issue that approach takes here:
There is no simple way of doing what you're intending, check out the Django code/template that generates the view:
@never_cache
def index(self, request, extra_context=None):
"""
Display the main admin index page, which lists all of the installed
apps that have been registered in this site.
"""
app_list = self.get_app_list(request)
context = {
**self.each_context(request),
'title': self.index_title,
'app_list': app_list,
**(extra_context or {}),
}
request.current_app = self.name
return TemplateResponse(request, self.index_template or 'admin/index.html', context)
templates/admin/index.html
:
{% for app in app_list %}
<div class="app-{{ app.app_label }} module">
<table>
<caption>
<a href="{{ app.app_url }}" class="section" title="{% blocktrans with name=app.name %}Models in the {{ name }} application{% endblocktrans %}">{{ app.name }}</a>
</caption>
{% for model in app.models %}
<tr class="model-{{ model.object_name|lower }}">
{% if model.admin_url %}
<th scope="row"><a href="{{ model.admin_url }}">{{ model.name }}</a></th>
{% else %}
<th scope="row">{{ model.name }}</th>
{% endif %}
{% if model.add_url %}
<td><a href="{{ model.add_url }}" class="addlink">{% trans 'Add' %}</a></td>
{% else %}
<td> </td>
{% endif %}
{% if model.admin_url %}
{% if model.view_only %}
<td><a href="{{ model.admin_url }}" class="viewlink">{% trans 'View' %}</a></td>
{% else %}
<td><a href="{{ model.admin_url }}" class="changelink">{% trans 'Change' %}</a></td>
{% endif %}
{% else %}
<td> </td>
{% endif %}
</tr>
{% endfor %}
</table>
</div>
{% endfor %}
As you can see, Django admin template is looping your app_list directy, so the only way of doing it would be to override the admin/index.html
template to place your models in your desired order.
From Django docs:
For those templates that cannot be overridden in this way, you may still override them for your entire project. Just place the new version in your templates/admin directory. This is particularly useful to create custom 404 and 500 pages.
https://docs.djangoproject.com/en/2.2/ref/contrib/admin/#overriding-vs-replacing-an-admin-template
这篇关于如何在Django Admin中将模型重新分配给其他应用程序以仅用于显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!