自定义Django管理员索引页面以显示模型对象

时间:2021-08-27 19:18:44

In the Django admin index page, the app and its models will normally be listed. How can the model objects also be listed in this index page? Instead of displaying just the app, I want to also display its model objects. How should it be customized?

在Django管理员索引页面中,通常会列出应用程序及其模型。如何在此索引页中列出模型对象?我想要显示其模型对象,而不是只显示应用程序。应该如何定制?

自定义Django管理员索引页面以显示模型对象

3 个解决方案

#1


12  

I wanted the same functionality for my site and added it by doing slight modifications to the core django system.

我想为我的网站提供相同的功能,并通过对核心django系统稍作修改来添加它。

Step 1: First we need a way to indicate which models should have their properties listed. Add the following code to the models for which you want the instances listed (in models.py):

第1步:首先,我们需要一种方法来指出哪些模型应列出其属性。将以下代码添加到您希望列出实例的模型中(在models.py中):

class Meta:
    list_instances = True

Step 2: We need to modify Django to recognize and read this new attribute. In core-django file: db/models/options.py, roughly at line 22 append 'list_instances' to DEFAULT_NAMES:

第2步:我们需要修改Django来识别和读取这个新属性。在core-django文件中:db / models / options.py,大致在第22行将'list_instances'附加到DEFAULT_NAMES:

DEFAULT_NAMES = ('verbose_name', 'verbose_name_plural', 'db_table', 'ordering',
             'unique_together', 'permissions', 'get_latest_by',
             'order_with_respect_to', 'app_label', 'db_tablespace',
             'abstract', 'managed', 'proxy', 'auto_created', 'list_instances')

and in the same file, roughly at line 52, create a default field for this attribute right after the other attributes :

并且在同一文件中,大致在第52行,在其他属性之后立即为此属性创建默认字段:

self.list_instances = False

Step 3: We need to pass this information along to the template that generates the index page. In core-django file: contrib/admin/sites.py, inside index() method and inside the "if has_module_perms:" part, add the following code:

第3步:我们需要将此信息传递给生成索引页面的模板。在core-django文件中:contrib / admin / sites.py,在index()方法内部和“if has_module_perms:”部分内,添加以下代码:

instances = []
if (model._meta.list_instances == True):
    instances = model_admin.queryset(None)

This will create the list of instances to show, but only if the list_instance attribute is set. In the same file, a few lines further down, append these values to the "model_dict" construct.

这将创建要显示的实例列表,但仅限于设置了list_instance属性。在同一个文件中,向下几行,将这些值附加到“model_dict”构造中。

model_dict = {
    'name': capfirst(model._meta.verbose_name_plural),
    'admin_url': mark_safe('%s/%s/' % (app_label, model.    __name__.lower())),
    'perms': perms,
    'list_instances':model._meta.list_instances,
    'instances': instances,
}

Step 4: The final step is to modify the template to support this. Either edit the core-django file /contrib/admin/templates/admin/index.html or copy this file to the templates/admin/ directory of your specific app. Add a few lines after the standard code for generating rows to generate the "sub-rows" if applicable. Roughly at line 40, right between "/tr>" and "{% endfor %}":

第4步:最后一步是修改模板以支持这一点。编辑core-django文件/contrib/admin/templates/admin/index.html或将此文件复制到特定应用程序的templates / admin /目录。在生成行的标准代码后面添加几行,以生成“子行”(如果适用)。大约在第40行,在“/ tr>”和“{%endfor%}”之间:

{% if model.list_instances %}
    {% for instance in model.instances %}
    <tr>
        <td colspan="2" style="padding-left: 2em;">{{ instance }}</td>
        {% if model.perms.change %}
            <td><a href="{{ model.admin_url }}{{ instance.id }}/" class="changelink">{% trans 'Change' %}</a></td>
        {% else %}
            <td>&nbsp;</td>
        {% endif %}
    </tr>
    {% endfor %}
{% endif %}

This will cause the item to be listed with the name generated by the unicode() method in the model.

这将导致项目列出模型中unicode()方法生成的名称。

Step 5: Lo and behold! It should look something like this:

第五步:看见!它应该看起来像这样:

自定义Django管理员索引页面以显示模型对象

Edit: Optional Step 6: If you want the instance names to be clickable too, just change the template (index.html) and replace:

编辑:可选步骤6:如果您希望实例名称也可以单击,只需更改模板(index.html)并替换:

<td colspan="2" style="padding-left: 2em;">{{ instance }}</td>

with:

有:

<td colspan="2" style="padding-left: 2em;">        
    {% if model.perms.change %}            
        <a href="{{ model.admin_url }}{{ instance.id}}">{{ instance }}</a>
    {% else %}
        {{ instance }}
    {% endif %}
</td>

#2


1  

You can do this by changing the various admin templates - the root one is called app_index.html and controls what gets displayed there. The best way to investigate what's happening where is to install django-debug-toolbar and then look at the templates being used for each view to figure out how to customise.

您可以通过更改各种管理模板来完成此操作 - 根模板称为app_index.html并控制在那里显示的内容。调查发生了什么的最好方法是安装django-debug-toolbar,然后查看每个视图使用的模板,以找出如何自定义。

#3


0  

UPDATE Setomidor answer for django 10

更新django 10的Setomidor答案

Always great to come back to this clean solution!

总是很高兴回到这个干净的解决方案!

step 2 - it is around line 125 (was 52)

第2步 - 它在第125行附近(是52)

step 3 - in sites.py - update the new method -

第3步 - 在sites.py中 - 更新新方法 -

_build_app_dict

inside the for loop : for model, model_admin in models.items():

在for循环中:对于model,model.adms()中的model_admin:

add step 3 as said around lines 430 and 460

如在线430和460周围所述添加步骤3

instances = []


if (model._meta.list_instances == True):
    instances = model_admin.get_queryset(None)

#1


12  

I wanted the same functionality for my site and added it by doing slight modifications to the core django system.

我想为我的网站提供相同的功能,并通过对核心django系统稍作修改来添加它。

Step 1: First we need a way to indicate which models should have their properties listed. Add the following code to the models for which you want the instances listed (in models.py):

第1步:首先,我们需要一种方法来指出哪些模型应列出其属性。将以下代码添加到您希望列出实例的模型中(在models.py中):

class Meta:
    list_instances = True

Step 2: We need to modify Django to recognize and read this new attribute. In core-django file: db/models/options.py, roughly at line 22 append 'list_instances' to DEFAULT_NAMES:

第2步:我们需要修改Django来识别和读取这个新属性。在core-django文件中:db / models / options.py,大致在第22行将'list_instances'附加到DEFAULT_NAMES:

DEFAULT_NAMES = ('verbose_name', 'verbose_name_plural', 'db_table', 'ordering',
             'unique_together', 'permissions', 'get_latest_by',
             'order_with_respect_to', 'app_label', 'db_tablespace',
             'abstract', 'managed', 'proxy', 'auto_created', 'list_instances')

and in the same file, roughly at line 52, create a default field for this attribute right after the other attributes :

并且在同一文件中,大致在第52行,在其他属性之后立即为此属性创建默认字段:

self.list_instances = False

Step 3: We need to pass this information along to the template that generates the index page. In core-django file: contrib/admin/sites.py, inside index() method and inside the "if has_module_perms:" part, add the following code:

第3步:我们需要将此信息传递给生成索引页面的模板。在core-django文件中:contrib / admin / sites.py,在index()方法内部和“if has_module_perms:”部分内,添加以下代码:

instances = []
if (model._meta.list_instances == True):
    instances = model_admin.queryset(None)

This will create the list of instances to show, but only if the list_instance attribute is set. In the same file, a few lines further down, append these values to the "model_dict" construct.

这将创建要显示的实例列表,但仅限于设置了list_instance属性。在同一个文件中,向下几行,将这些值附加到“model_dict”构造中。

model_dict = {
    'name': capfirst(model._meta.verbose_name_plural),
    'admin_url': mark_safe('%s/%s/' % (app_label, model.    __name__.lower())),
    'perms': perms,
    'list_instances':model._meta.list_instances,
    'instances': instances,
}

Step 4: The final step is to modify the template to support this. Either edit the core-django file /contrib/admin/templates/admin/index.html or copy this file to the templates/admin/ directory of your specific app. Add a few lines after the standard code for generating rows to generate the "sub-rows" if applicable. Roughly at line 40, right between "/tr>" and "{% endfor %}":

第4步:最后一步是修改模板以支持这一点。编辑core-django文件/contrib/admin/templates/admin/index.html或将此文件复制到特定应用程序的templates / admin /目录。在生成行的标准代码后面添加几行,以生成“子行”(如果适用)。大约在第40行,在“/ tr>”和“{%endfor%}”之间:

{% if model.list_instances %}
    {% for instance in model.instances %}
    <tr>
        <td colspan="2" style="padding-left: 2em;">{{ instance }}</td>
        {% if model.perms.change %}
            <td><a href="{{ model.admin_url }}{{ instance.id }}/" class="changelink">{% trans 'Change' %}</a></td>
        {% else %}
            <td>&nbsp;</td>
        {% endif %}
    </tr>
    {% endfor %}
{% endif %}

This will cause the item to be listed with the name generated by the unicode() method in the model.

这将导致项目列出模型中unicode()方法生成的名称。

Step 5: Lo and behold! It should look something like this:

第五步:看见!它应该看起来像这样:

自定义Django管理员索引页面以显示模型对象

Edit: Optional Step 6: If you want the instance names to be clickable too, just change the template (index.html) and replace:

编辑:可选步骤6:如果您希望实例名称也可以单击,只需更改模板(index.html)并替换:

<td colspan="2" style="padding-left: 2em;">{{ instance }}</td>

with:

有:

<td colspan="2" style="padding-left: 2em;">        
    {% if model.perms.change %}            
        <a href="{{ model.admin_url }}{{ instance.id}}">{{ instance }}</a>
    {% else %}
        {{ instance }}
    {% endif %}
</td>

#2


1  

You can do this by changing the various admin templates - the root one is called app_index.html and controls what gets displayed there. The best way to investigate what's happening where is to install django-debug-toolbar and then look at the templates being used for each view to figure out how to customise.

您可以通过更改各种管理模板来完成此操作 - 根模板称为app_index.html并控制在那里显示的内容。调查发生了什么的最好方法是安装django-debug-toolbar,然后查看每个视图使用的模板,以找出如何自定义。

#3


0  

UPDATE Setomidor answer for django 10

更新django 10的Setomidor答案

Always great to come back to this clean solution!

总是很高兴回到这个干净的解决方案!

step 2 - it is around line 125 (was 52)

第2步 - 它在第125行附近(是52)

step 3 - in sites.py - update the new method -

第3步 - 在sites.py中 - 更新新方法 -

_build_app_dict

inside the for loop : for model, model_admin in models.items():

在for循环中:对于model,model.adms()中的model_admin:

add step 3 as said around lines 430 and 460

如在线430和460周围所述添加步骤3

instances = []


if (model._meta.list_instances == True):
    instances = model_admin.get_queryset(None)