suit_extensions是一个用于扩展及优化django-suit功能的应用
-
pip install git+https://github.com/kliyes/suit_extensions@master
-
INSTALLED_APPS
配置:INSTALLED_APPS = [ 'suit_extensions', 'suit', 'django.contrib.admin', ... ]
-
增加
SUIT_CONFIG
配置项,内容参考django-suit配置,新增了3个配置项,分别为:{ # 是否根据自定义MENU来生成面包屑 "GENERATE_BREADCRUMBS_FROM_MENU": True, # 是否隐藏对象修改界面的工具箱 "HIDE_OBJECT_TOOLS": True, # 是否隐藏对象修改界面的"保存并..."按钮 "HIDE_SAVE_AND_MORE_BUTTON": True }
####ExtensionModelAdminMixin
同时继承ExtensionModelAdminMixin
和ModelAdmin
from django.contrib import admin
from suit_extensions.mixins import ExtensionModelAdminMixin
@admin.register(ExampleModel)
class ExampleAdmin(ExtensionModelAdminMixin, admin.ModelAdmin):
pass
suit_extensions
已经对默认的admin.UserAdmin
和admin.GroupAdmin
做了修改,如果需要进一步自定义:
from django.contrib.auth import get_user_model
from suit_extensions.admin import ExtendedUserAdmin
UserModel = get_user_model()
class MyUserAdmin(ExtendedUserAdmin):
pass
admin.site.unregister(UserModel)
admin.site.register(UserModel, MyUserAdmin)
在列表页面每一项最后增加一个操作列,默认包括修改和删除按钮:
from suit_extensions.mixins import OperationModelAdminMixin
@admin.register(ExampleModel)
class ExampleAdmin(OperationModelAdminMixin, admin.ModelAdmin):
pass
也可修改operations_list
属性添加自定义操作
django
自带的日期/时间选择器十分不人性化,比如无法快速切换年份(只能逐月选择),suit_extensions
使用datepicker
和timepicker
进行了重写,效果如下:
编写form
:
# forms.py
from django import forms
from django.contrib.auth import get_user_model
from suit_extensions.widgets import DateTimePickerWidget
UserModel = get_user_model()
class UserAdminForm(forms.ModelForm):
class Meta:
model = UserModel
fields = "__all__"
widgets = {
"last_login": DateTimePickerWidget(
datepicker_options={"orientation": "top"}
),
"date_joined": DateTimePickerWidget()
}
admin
指定使用上述form
:
# admin.py
from django.contrib import admin
from django.contrib.auth import get_user_model
from suit_extensions.admin import ExtendedUserAdmin
from .form import UserAdminForm
UserModel = get_user_model()
class UserAdmin(ExtendedUserAdmin):
form = UserAdminForm
admin.site.unregister(UserModel)
admin.site.register(UserModel, UserAdmin)
DateTimePickerWidget
支持两个初始化dict
类型参数:datepicker_options
和timepicker_options
,具体参数值请参考:
datepicker
:https://bootstrap-datepicker.readthedocs.io/en/latest/options.html
timepicker
:http://jdewit.github.io/bootstrap-timepicker
- 利用
MENU
配置项进行面包屑配置,并使其保持和左侧菜单栏完全一致; - 引入
datepicker
和timepicker
js库替换django
自带的时间日期选择器;