自定义Django管理员索引页面以显示模型对象 [英] Customize Django admin index page to display model objects

查看:157
本文介绍了自定义Django管理员索引页面以显示模型对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



解决方案

我想要与我的网站相同的功能,并通过对核心django系统进行轻微的修改来添加它。 >

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

 类Meta:
list_instances = True

步骤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')



在同一个文件中,大致在第52行,在其他属性之后创建一个该属性的默认字段:

  self.list_instances = False 

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

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

这将创建要显示的实例列表,但只有设置了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':实例,
}

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

  {%if model。 list_instances%} 
{%,例如在model.instances%}
< tr>
< td colspan =2style =padding-left:2em> {{instance}}< / td>
{%if model.perms.change%}
< td>< a href ={{model.admin_url}} {{instance.id}} /class =changelink> ; {%trans'更改'%}< / a>< / td>
{%else%}
< td>& nbsp;< / td>
{%endif%}
< / tr>
{%endfor%}
{%endif%}

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



步骤5
Lo和看!它应该是这样的:





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

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

with:

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


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?

解决方案

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

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):

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:

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 :

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:

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.

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 %}":

{% 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.

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

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

<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>

这篇关于自定义Django管理员索引页面以显示模型对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆