简体   繁体   English

如何在Django管理员中以表格格式显示添加模型?

[英]How do I display add model in tabular format in the Django admin?

I'm just starting out with Django writing my first app - a chore chart manager for my family. 我刚刚开始使用Django编写我的第一个应用程序 - 我的家人的家务管理经理。 In the tutorial it shows you how to add related objects in a tabular form. 在本教程中,它将向您展示如何以表格形式添加相关对象 I don't care about the related objects, I just want to add the regular object in a tabular form. 我不关心相关的对象,我只想以表格形式添加常规对象。 This is what I have in my admin.py 这就是我在admin.py中的内容

from chores.models import Chore
from django.contrib import admin

class ChoreAdmin(admin.ModelAdmin):
    fieldsets = [ 
        (None,              {'fields': ['description', 'frequency', 'person']})
    ]   

admin.site.register(Chore, ChoreAdmin)

and I want when I click "add chore" that rather than seeing: 我想点击“添加家务”而不是看到:

Description: _____
Frequency: ______
Person: _____

I want it to show: 我想要它显示:

Description: __________ | Frequency: _______ | Person: _____

Is this trivial to do, or would it take a lot of custom effort? 这是微不足道的,还是需要大量的定制工作? And if it is easy, how do I do it? 如果这很容易,我该怎么做?

Thanks! 谢谢!

OP is probably all set, but for new users reading this, refer to: https://docs.djangoproject.com/en/dev/ref/contrib/admin/ OP可能已全部设置,但对于阅读此内容的新用户,请参阅: https//docs.djangoproject.com/en/dev/ref/contrib/admin/

Basically following part in above link: 基本上是上面的链接部分:


The field_options dictionary can have the following keys: field_options字典可以包含以下键:

fields : A tuple of field names to display in this fieldset. fields :要在此字段集中显示的字段名称元组。 This key is required. 这个密钥是必需的。

Example: 例:

{
'fields': ('first_name', 'last_name', 'address', 'city', 'state'),
}

Just like with the fields option, to display multiple fields on the same line, wrap those fields in their own tuple. 就像使用fields选项一样,要在同一行显示多个字段,请将这些字段包装在自己的元组中。 In this example, the first_name and last_name fields will display on the same line: 在此示例中, first_namelast_name字段将显示在同一行上:

{
'fields': (('first_name', 'last_name'), 'address', 'city', 'state'),
}

Something to try, 有事要试试,

class ChoreAdmin(admin.ModelAdmin):
 list_display = ('description', 'frequency', 'person')
 list_editable = ('description', 'frequency', 'person') 

Which should enable you to edit all your entries in a tabular form (if I've read the docs correctly)... 这应该使您能够以表格形式编辑所有条目(如果我已正确阅读文档)...

As far as I know, there's not a default ModelAdmin option for this, but you could change the CSS of the admin site or modify the layout of change_form.html on a per-model basis. 据我所知,没有默认的ModelAdmin选项,但您可以更改管理站点的CSS或基于每个模型修改change_form.html的布局。

You could modify the admin site to use your custom CSS (on a per-model basis by subclassing the ModelAdmin with a Media class) and modify it (probably making use of the body.change-form CSS class) and make sure that the fieldsets are next to each other. 您可以修改管理站点以使用您的自定义CSS(在每个模型的基础上通过使用MediaModelAdmin )并修改它(可能使用body.change-form CSS类)并确保body.change-form彼此相邻。

You could also create a template inside your templates directory with the name /admin/chores/chore/change_form.html . 您还可以在模板目录中创建名称为/admin/chores/chore/change_form.html模板。 Unfortunately, the part that creates the actual form elements is not in a seperate block, so you should override the ' content ' block with your custom contents, copying a whole lot of the django/contrib/admin/templates/admin/change_form.html file. 不幸的是,创建实际表单元素的部分不在单独的块中,因此您应该使用自定义内容覆盖“ content ”块,复制大量的django/contrib/admin/templates/admin/change_form.html文件。

Please refer to relevent documentation for more information on this. 有关此内容的更多信息,请参阅相关文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM