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

时间:2022-06-01 22:55:47

I'm just starting out with Django writing my first app - a chore chart manager for my family. 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

我刚刚开始使用Django编写我的第一个应用程序 - 我的家庭的家务管理经理。在本教程中,它将向您展示如何以表格形式添加相关对象。我不关心相关的对象,我只想以表格形式添加常规对象。这是我在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!

3 个解决方案

#1


6  

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. This key is required.

fields:要在此字段集中显示的字段名称元组。这个密钥是必需的。

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. In this example, the first_name and last_name fields will display on the same line:

就像使用fields选项一样,要在同一行显示多个字段,请将这些字段包装在自己的元组中。在此示例中,first_name和last_name字段将显示在同一行上:

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

#2


2  

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

这应该使您能够以表格形式编辑所有条目(如果我已正确阅读文档)...

#3


0  

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(在每个模型的基础上通过将ModelAdmin与Media类继承)并修改它(可能使用body.change-form CSS类)并确保字段集彼此相邻。

You could also create a template inside your templates directory with the name /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.

您还可以在模板目录中创建名称为/admin/chores/chore/change_form.html的模板。不幸的是,创建实际表单元素的部分不是单独的块,因此您应该使用自定义内容覆盖“内容”块,复制大量的django / contrib / admin / templates / admin / change_form.html文件。

Please refer to relevent documentation for more information on this.

有关此内容的更多信息,请参阅相关文档。

#1


6  

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. This key is required.

fields:要在此字段集中显示的字段名称元组。这个密钥是必需的。

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. In this example, the first_name and last_name fields will display on the same line:

就像使用fields选项一样,要在同一行显示多个字段,请将这些字段包装在自己的元组中。在此示例中,first_name和last_name字段将显示在同一行上:

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

#2


2  

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

这应该使您能够以表格形式编辑所有条目(如果我已正确阅读文档)...

#3


0  

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(在每个模型的基础上通过将ModelAdmin与Media类继承)并修改它(可能使用body.change-form CSS类)并确保字段集彼此相邻。

You could also create a template inside your templates directory with the name /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.

您还可以在模板目录中创建名称为/admin/chores/chore/change_form.html的模板。不幸的是,创建实际表单元素的部分不是单独的块,因此您应该使用自定义内容覆盖“内容”块,复制大量的django / contrib / admin / templates / admin / change_form.html文件。

Please refer to relevent documentation for more information on this.

有关此内容的更多信息,请参阅相关文档。