116 浏览 6 months, 2 weeks

9 后台管理 Admin

版权声明: 转载请注明出处



  • list_display 列表显示字段
  • list_editable 列表可编辑字段,必须在list_display已定义
  • list_filter 可过滤字段
  • list_display_links 添加超链接的字段
  • search_fields 搜索字段
  • ordering 这是一个列表,按这个列表的顺序排序
  • list_per_page 每页最大显示条目
  • list_max_show_all

  • view_on_site 有重定向到页面的超链接,可以通过重写view_on_site指定跳转链接

    def view_on_site(self, obj):
        url = reverse('dormevaluation_detail', kwargs={'pk':})
        return url
  • form 指定表单

  • inlines 内联formset

    • extra 只针对内容formset,表示新添加form显示的个数
options Description
actions A list of actions to make available on the change list page.
exclude This attribute, if given, should be a list of field names to exclude from the form.
fields Use the fields option to make simple layout changes in the forms on the “add” and “change” pages such as showing only a subset of available fields, modifying their order, or grouping them into rows.
fieldsets Set fieldsets to control the layout of admin “add” and “change” pages.
filter_horizontal By default, a ManyToManyField is displayed in the admin site with a <select multiple>. However, multiple-select boxes can be difficult to use when selecting many items. Adding a ManyToManyField to this list will instead use a nifty unobtrusive JavaScript “filter” interface that allows searching within the options. The unselected and selected options appear in two boxes side by side.
filter_vertical Same as filter_horizontal, but uses a vertical display of the filter interface with the box of unselected options appearing above the box of selected options.
form By default a ModelForm is dynamically created for your model. It is used to create the form presented on both the add/change pages. You can easily provide your own ModelForm to override any default form behavior on the add/change pages. Alternatively, you can customize the default form rather than specifying an entirely new one by using the ModelAdmin.get_form() method.
formfield_overrides This provides a quick-and-dirty way to override some of the Field options for use in the admin. formfield_overrides is a dictionary mapping a field class to a dict of arguments to pass to the field at construction time.
list_display_links Use list_display_links to control if and which fields in list_display should be linked to the “change” page for an object.
list_editable Set list_editable to a list of field names on the model which will allow editing on the change list page. That is, fields listed in list_editable will be displayed as form widgets on the change list page, allowing users to edit and save multiple rows at once.
list_filter Set list_filter to activate filters in the right sidebar of the change list page of the admin
list_max_show_all Set list_max_show_all to control how many items can appear on a “Show all” admin change list page. The admin will display a “Show all” link on the change list only if the total result count is less than or equal to this setting. By default, this is set to 200.
list_per_page Set list_per_page to control how many items appear on each paginated admin change list page. By default, this is set to 100.
paginator The paginator class to be used for pagination. By default, django.core.paginator.Paginator is used. If the custom paginator class doesn’t have the same constructor interface as django.core.paginator.Paginator, you will also need to provide an implementation for ModelAdmin.get_paginator().
prepopulated_fields Set prepopulated_fields to a dictionary mapping field names to the fields it should prepopulate from:
preserve_filters The admin now preserves filters on the list view after creating, editing or deleting an object. You can restore the previous behavior of clearing filters by setting this attribute to False.
radio_fields By default, Django’s admin uses a select-box interface (<select>) for fields that are ForeignKey or have choices set. If a field is present in radio_fields, Django will use a radio-button interface instead.
raw_id_fields By default, Django’s admin uses a select-box interface (<select>) for fields that are ForeignKey. Sometimes you don’t want to incur the overhead of having to select all the related instances to display in the drop-down. <br> raw_id_fields is a list of fields you would like to change into an Input widget for either a ForeignKey or ManyToManyField:
readonly_fields By default the admin shows all fields as editable. Any fields in this option (which should be a list or tuple) will display its data as-is and non-editable; they are also excluded from the ModelForm used for creating and editing. Note that when specifying ModelAdmin.fields or ModelAdmin.fieldsets the read-only fields must be present to be shown (they are ignored otherwise).
save_as Set save_as to enable a “save as new” feature on admin change forms.
save_on_top Set save_on_top to add save buttons across the top of your admin change forms.
search_field Set search_fields to enable a search box on the admin change list page. This should be set to a list of field names that will be searched whenever somebody submits a search query in that text box.
show_full_result_count Set show_full_result_count to control whether the full count of objects should be displayed on a filtered admin page (e.g. 99 results (103 total)).
view_on_site Set view_on_site to control whether or not to display the “View on site” link. This link should bring you to a URL where you can display the saved object. This value can be either a boolean flag or a callable. If True (the default), the object’s get_absolute_url() method will be used to generate the url.
Custom template options
options Description
add_form_template Path to a custom template, used by add_view().
change_list_template Path to a custom template, used by changelist_view().
change_form_template Path to a custom template, used by change_view().
delete_confirmation_template Path to a custom template, used by delete_view() for displaying a confirmation page when deleting one or more objects.
delete_selected_confirmation_template Path to a custom template, used by the delete_selected action method for displaying a confirmation page when deleting one or more objects.
object_history_template Path to a custom template, used by history_view().


ImproperlyConfigured : There are duplicate field(s) in RailPackageAdmin.fieldsets

Fieldset sequence in admin should align with model definition

Site matching query does not exist

第一种办法是:编辑 文件,从 INSTALLED_APPS 配置项中移除 'django.contrib.sites',。

第二种办法:通过 Python shell 为 Site model 添加一条记录(将网站的域名添加进去):

$ python shell

from django.contrib.sites.models import Site
Site.objects.create(pk=1, domain='', name='')