Skip to content

Latest commit

 

History

History
153 lines (95 loc) · 3.69 KB

README.md

File metadata and controls

153 lines (95 loc) · 3.69 KB

suit_extensions

suit_extensions是一个用于扩展及优化django-suit功能的应用

安装步骤

  1. pip install git+https://github.com/kliyes/suit_extensions@master

  2. INSTALLED_APPS配置:

    INSTALLED_APPS = [
        'suit_extensions',
        'suit',
        'django.contrib.admin',
        ...
    ]
  3. 增加SUIT_CONFIG配置项,内容参考django-suit配置,新增了3个配置项,分别为:

    {
        # 是否根据自定义MENU来生成面包屑
        "GENERATE_BREADCRUMBS_FROM_MENU": True,
        # 是否隐藏对象修改界面的工具箱
        "HIDE_OBJECT_TOOLS": True,
        # 是否隐藏对象修改界面的"保存并..."按钮
        "HIDE_SAVE_AND_MORE_BUTTON": True
    }

使用方法

admin class

####ExtensionModelAdminMixin

同时继承ExtensionModelAdminMixinModelAdmin

from django.contrib import admin

from suit_extensions.mixins import ExtensionModelAdminMixin


@admin.register(ExampleModel)
class ExampleAdmin(ExtensionModelAdminMixin, admin.ModelAdmin):
    pass

suit_extensions已经对默认的admin.UserAdminadmin.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)

OperationModelAdminMixin

在列表页面每一项最后增加一个操作列,默认包括修改和删除按钮:

from suit_extensions.mixins import OperationModelAdminMixin


@admin.register(ExampleModel)
class ExampleAdmin(OperationModelAdminMixin, admin.ModelAdmin):
    pass

也可修改operations_list属性添加自定义操作

datepicker/timepicker

django自带的日期/时间选择器十分不人性化,比如无法快速切换年份(只能逐月选择),suit_extensions使用datepickertimepicker进行了重写,效果如下:

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_optionstimepicker_options,具体参数值请参考:

datepickerhttps://bootstrap-datepicker.readthedocs.io/en/latest/options.html

timepickerhttp://jdewit.github.io/bootstrap-timepicker

功能列表

  1. 利用MENU配置项进行面包屑配置,并使其保持和左侧菜单栏完全一致;
  2. 引入datepickertimepickerjs库替换django自带的时间日期选择器;