一、Xadmin------安装
翻译:http://xadmin.readthedocs.io/en/docs-chinese
1、安装方法:
1)pip install django-xadmin
2)通过源文件安装,我是通过这种方式安装的
https://github.com/sshwsfc/django-xadmin 下载源文件
运行pip install -r requirement.txt 安装依赖包
3)新建文件夹 extra_apps,将下载的xadmin源文件复制进去(如果是通过步骤一安装的则不需要)
4)将xadmin及相关依赖添加到setting.py的INSTALLED_APP中
INSTALLED_APPS = (
'xadmin',
‘crispy_forms’,
'reversion',
)
5)设置url
from django.config.urls import url, include
import xadmin
urlpatterns = patterns(
url(r'^xadmin/', include(xadmin.site.urls))
)
6)在各个app下面,新建adminx.py 文件
在adminx.py中注册自己创建的model
class GoodsAdmin(object): list_display = ["name", "click_num", "sold_num", "fav_num", "goods_num", "market_price","shop_price", "goods_brief", "goods_desc", "is_new", "is_hot", "add_time"] search_fields = ['name', ] list_filter = ["name", "click_num", "sold_num", "fav_num", "goods_num", "market_price","shop_price", "is_new", "is_hot", "add_time", "category__name"] style_fields = {"goods_desc": "ueditor"} show_all_rel_details = True class GoodsBrandAdmin(object): list_display = ["category", "image", "name", "desc"] # 实现过滤出所有的一级类目 def get_context(self): context = super(GoodsBrandAdmin, self).get_context() if "form" in context: context["form"].fields["category"].queryset = GoodsCategory.objects.filter(category_type=1) return context # 将model和xadmin关联注册到xadmin中 xadmin.site.register(Goods, GoodsAdmin) xadmin.site.register(GoodsCategoryBrand, GoodsBrandAdmin)
7)UserProfile的注册:
settings.py中,使用我们自定义的UserProfile
AUTH_USER_MODEL = 'users.UserProfile'
在自定义model user的xadmin文件中,可以自定义xadmin相关设置,重写相关方法,如xadmin头尾部设计,自定义菜单排序等等