xadmin与django-rest-framework的集成(1)
什么是xadmin?什么是django-rest-framework?
xadmin是开源的一个类似于django自带的后台管理系统admin的开源模块,它基于bootstrap3框架,内置强大的插件系统,根据项目需求可以自定义扩展,它比admin功能更加丰富,更加便于我们项目的开发。
关于django-rest-framework是基于restful API标准而开发的一套针对django框架的api框架,作为目前流行的前后端分离架构,django-rest-framework通过强大的标准api接口,以及api文档自动撰写功能等对我们后端项目开发就是一种福音.
搭建开发环境
本次演示基于python3.6,django2.0(目前xadmin已经支持django2.0+)
创建虚拟环境并激活进入虚拟环境
virtualenv xadminenv
source xadminenv/bin/active
进入python环境可以看到,我们虚拟环境默认安装的python环境为3.6的
安装django,这里我们加上豆瓣源,https://pypi.douban.com/simple/
pip3 install -i https://pypi.douban.com/simple/ django
使用pip list命令可以看到,我使用的django为2.0版本
创建项目
创建项目目录
django-admin startproject myproj
项目结构如下:
myproj / manage.py myproj / __init__ .py setting.py url.py wsgi.PY
关于django的其它用法见我的关于django使用的文章,这里我们着重研究xadmin的使用
下面几步仅仅是为了演示需求
我们创建几个应用
python manage.py startapp users
python manage.py startapp trade
python manage.py startapp goods
python manage.py startapp user_operateion
我们在manage.py同级创建一个apps的文件夹用于存放我们的各个应用,同时创建一个extra_apps用于存放第三方包,将我们的应用放入相应的包
settings.py配置
import os import sys # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0,os.path.join(BASE_DIR,'apps')) #将应用包加入系统变量,便于模块导入 sys.path.insert(0,os.path.join(BASE_DIR,'extra_apps')) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = ')57n02l@w9p9)g(47pcp6+uofk$&-a_eqburb%r2n$#w751^fa' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True ALLOWED_HOSTS = [] # Application definition INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'users', #注册app 'goods', 'trade', 'user_operation', ]
一切就绪,编写我们的model
users/models.py
1 from datetime import datetime 2 3 from django.db import models 4 from django.contrib.auth.models import AbstractUser 5 6 # Create your models here. 7 8 9 class UserProfile(AbstractUser): 10 """ 11 用户 12 """ 13 name = models.CharField(max_length=30, null=True, blank=True, verbose_name="姓名") 14 birthday = models.DateField(null=True, blank=True, verbose_name="出生年月") 15 gender = models.CharField(max_length=6, choices=(("male", u"男"), ("female", "女")), default="female", verbose_name="性别") 16 mobile = models.CharField(null=True, blank=True, max_length=11, verbose_name="电话") 17 email = models.EmailField(max_length=100, null=True, blank=True, verbose_name="邮箱") 18 19 class Meta: 20 verbose_name = "用户" 21 verbose_name_plural = verbose_name 22 23 def __str__(self): 24 return self.username 25 26 27 class VerifyCode(models.Model): 28 """ 29 短信验证码 30 """ 31 code = models.CharField(max_length=10, verbose_name="验证码") 32 mobile = models.CharField(max_length=11, verbose_name="电话") 33 add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间") 34 35 class Meta: 36 verbose_name = "短信验证码" 37 verbose_name_plural = verbose_name 38 39 def __str__(self): 40 return self.code
goos/models.py
1 from datetime import datetime 2 3 from django.db import models 4 from DjangoUeditor.models import UEditorField 5 # Create your models here. 6 7 8 class GoodsCategory(models.Model): 9 """ 10 商品类别 11 """ 12 CATEGORY_TYPE = ( 13 (1, "一级类目"), 14 (2, "二级类目"), 15 (3, "三级类目"), 16 ) 17 18 name = models.CharField(default="", max_length=30, verbose_name="类别名", help_text="类别名") 19 code = models.CharField(default="", max_length=30, verbose_name="类别code", help_text="类别code") 20 desc = models.TextField(default="", verbose_name="类别描述", help_text="类别描述") 21 category_type = models.IntegerField(choices=CATEGORY_TYPE, verbose_name="类目级别", help_text="类目级别") 22 parent_category = models.ForeignKey("self", null=True, blank=True, verbose_name="父类目级别", help_text="父目录", 23 related_name="sub_cat",on_delete=models.CASCADE) 24 is_tab = models.BooleanField(default=False, verbose_name="是否导航", help_text="是否导航") 25 add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间") 26 27 class Meta: 28 verbose_name = "商品类别" 29 verbose_name_plural = verbose_name 30 31 def __str__(self): 32 return self.name 33 34 35 class GoodsCategoryBrand(models.Model): 36 """ 37 品牌名 38 """ 39 category = models.ForeignKey(GoodsCategory, related_name='brands', null=True, blank=True, verbose_name="商品类目",on_delete=models.CASCADE) 40 name = models.CharField(default="", max_length=30, verbose_name="品牌名", help_text="品牌名") 41 desc = models.TextField(default="", max_length=200, verbose_name="品牌描述", help_text="品牌描述") 42 image = models.ImageField(max_length=200, upload_to="brands/") 43 add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间") 44 45 class Meta: 46 verbose_name = "品牌" 47 verbose_name_plural = verbose_name 48 db_table = "goods_goodsbrand" 49 50 def __str__(self): 51 return self.name 52 53 54 class Goods(models.Model): 55 """ 56 商品 57 """ 58 category = models.ForeignKey(GoodsCategory, verbose_name="商品类目",on_delete=models.CASCADE) 59 goods_sn = models.CharField(max_length=50, default="", verbose_name="商品唯一货号") 60 name = models.CharField(max_length=100, verbose_name="商品名") 61 click_num = models.IntegerField(default=0, verbose_name="点击数") 62 sold_num = models.IntegerField(default=0, verbose_name="商品销售量") 63 fav_num = models.IntegerField(default=0, verbose_name="收藏数") 64 goods_num = models.IntegerField(default=0, verbose_name="库存数") 65 market_price = models.FloatField(default=0, verbose_name="市场价格") 66 shop_price = models.FloatField(default=0, verbose_name="本店价格") 67 goods_brief = models.TextField(max_length=500, verbose_name="商品简短描述") 68 goods_desc = UEditorField(verbose_name=u"内容", imagePath="goods/images/", width=1000, height=300, 69 filePath="goods/files/", default='') 70 ship_free = models.BooleanField(default=True, verbose_name="是否承担运费") 71 goods_front_image = models.ImageField(upload_to="goods/images/", null=True, blank=True, verbose_name="封面图") 72 is_new = models.BooleanField(default=False, verbose_name="是否新品") 73 is_hot = models.BooleanField(default=False, verbose_name="是否热销") 74 add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间") 75 76 class Meta: 77 verbose_name = '商品' 78 verbose_name_plural = verbose_name 79 80 def __str__(self): 81 return self.name 82 83 84 class IndexAd(models.Model): 85 category = models.ForeignKey(GoodsCategory, related_name='category',verbose_name="商品类目",on_delete=models.CASCADE) 86 goods =models.ForeignKey(Goods, related_name='goods',on_delete=models.CASCADE) 87 88 class Meta: 89 verbose_name = '首页商品类别广告' 90 verbose_name_plural = verbose_name 91 92 def __str__(self): 93 return self.goods.name 94 95 96 class GoodsImage(models.Model): 97 """ 98 商品轮播图 99 """ 100 goods = models.ForeignKey(Goods, verbose_name="商品", related_name="images",on_delete=models.CASCADE) 101 image = models.ImageField(upload_to="", verbose_name="图片", null=True, blank=True) 102 add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间") 103 104 class Meta: 105 verbose_name = '商品图片' 106 verbose_name_plural = verbose_name 107 108 def __str__(self): 109 return self.goods.name 110 111 112 class Banner(models.Model): 113 """ 114 轮播的商品 115 """ 116 goods = models.ForeignKey(Goods, verbose_name="商品",on_delete=models.CASCADE) 117 image = models.ImageField(upload_to='banner', verbose_name="轮播图片") 118 index = models.IntegerField(default=0, verbose_name="轮播顺序") 119 add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间") 120 121 class Meta: 122 verbose_name = '轮播商品' 123 verbose_name_plural = verbose_name 124 125 def __str__(self): 126 return self.goods.name 127 128 129 class HotSearchWords(models.Model): 130 """ 131 热搜词 132 """ 133 keywords = models.CharField(default="", max_length=20, verbose_name="热搜词") 134 index = models.IntegerField(default=0, verbose_name="排序") 135 add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间") 136 137 class Meta: 138 verbose_name = '热搜词' 139 verbose_name_plural = verbose_name 140 141 def __str__(self): 142 return self.keywords
trade/models.py
1 from datetime import datetime 2 3 from django.db import models 4 from django.contrib.auth import get_user_model 5 6 from goods.models import Goods 7 User = get_user_model() 8 # Create your models here. 9 10 11 class ShoppingCart(models.Model): 12 """ 13 购物车 14 """ 15 user = models.ForeignKey(User, verbose_name=u"用户",on_delete=models.CASCADE) 16 goods = models.ForeignKey(Goods, verbose_name=u"商品",on_delete=models.CASCADE) 17 nums = models.IntegerField(default=0, verbose_name="购买数量") 18 19 add_time = models.DateTimeField(default=datetime.now, verbose_name=u"添加时间") 20 21 class Meta: 22 verbose_name = '购物车' 23 verbose_name_plural = verbose_name 24 unique_together = ("user", "goods") 25 26 def __str__(self): 27 return "%s(%d)".format(self.goods.name, self.nums) 28 29 30 class OrderInfo(models.Model): 31 """ 32 订单 33 """ 34 ORDER_STATUS = ( 35 ("TRADE_SUCCESS", "成功"), 36 ("TRADE_CLOSED", "超时关闭"), 37 ("WAIT_BUYER_PAY", "交易创建"), 38 ("TRADE_FINISHED", "交易结束"), 39 ("paying", "待支付"), 40 ) 41 42 user = models.ForeignKey(User, verbose_name="用户",on_delete=models.CASCADE) 43 order_sn = models.CharField(max_length=30, null=True, blank=True, unique=True, verbose_name="订单号") 44 trade_no = models.CharField(max_length=100, unique=True, null=True, blank=True, verbose_name=u"交易号") 45 pay_status = models.CharField(choices=ORDER_STATUS, default="paying", max_length=30, verbose_name="订单状态") 46 post_script = models.CharField(max_length=200, verbose_name="订单留言") 47 order_mount = models.FloatField(default=0.0, verbose_name="订单金额") 48 pay_time = models.DateTimeField(null=True, blank=True, verbose_name="支付时间") 49 50 # 用户信息 51 address = models.CharField(max_length=100, default="", verbose_name="收货地址") 52 signer_name = models.CharField(max_length=20, default="", verbose_name="签收人") 53 singer_mobile = models.CharField(max_length=11, verbose_name="联系电话") 54 55 add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间") 56 57 class Meta: 58 verbose_name = u"订单" 59 verbose_name_plural = verbose_name 60 61 def __str__(self): 62 return str(self.order_sn) 63 64 class OrderGoods(models.Model): 65 """ 66 订单的商品详情 67 """ 68 order = models.ForeignKey(OrderInfo, verbose_name="订单信息", related_name="goods",on_delete=models.CASCADE) 69 goods = models.ForeignKey(Goods, verbose_name="商品",on_delete=models.CASCADE) 70 goods_num = models.IntegerField(default=0, verbose_name="商品数量") 71 72 add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间") 73 74 class Meta: 75 verbose_name = "订单商品" 76 verbose_name_plural = verbose_name 77 78 def __str__(self): 79 return str(self.order.order_sn)
user_operation/models.py
from datetime import datetime from django.db import models from django.contrib.auth import get_user_model from goods.models import Goods # Create your models here. User = get_user_model() class UserFav(models.Model): """ 用户收藏 """ user = models.ForeignKey(User, verbose_name="用户",on_delete=models.CASCADE) goods = models.ForeignKey(Goods, verbose_name="商品", help_text="商品id",on_delete=models.CASCADE) add_time = models.DateTimeField(default=datetime.now, verbose_name=u"添加时间") class Meta: verbose_name = '用户收藏' verbose_name_plural = verbose_name unique_together = ("user", "goods") def __str__(self): return self.user.username class UserLeavingMessage(models.Model): """ 用户留言 """ MESSAGE_CHOICES = ( (1, "留言"), (2, "投诉"), (3, "询问"), (4, "售后"), (5, "求购") ) user = models.ForeignKey(User, verbose_name="用户",on_delete=models.CASCADE) message_type = models.IntegerField(default=1, choices=MESSAGE_CHOICES, verbose_name="留言类型", help_text=u"留言类型: 1(留言),2(投诉),3(询问),4(售后),5(求购)") subject = models.CharField(max_length=100, default="", verbose_name="主题") message = models.TextField(default="", verbose_name="留言内容", help_text="留言内容") file = models.FileField(upload_to="message/images/", verbose_name="上传的文件", help_text="上传的文件") add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间") class Meta: verbose_name = "用户留言" verbose_name_plural = verbose_name def __str__(self): return self.subject class UserAddress(models.Model): """ 用户收货地址 """ user = models.ForeignKey(User, verbose_name="用户" ,on_delete=models.CASCADE) province = models.CharField(max_length=100, default="", verbose_name="省份") city = models.CharField(max_length=100, default="", verbose_name="城市") district = models.CharField(max_length=100, default="", verbose_name="区域") address = models.CharField(max_length=100, default="", verbose_name="详细地址") signer_name = models.CharField(max_length=100, default="", verbose_name="签收人") signer_mobile = models.CharField(max_length=11, default="", verbose_name="电话") add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间") class Meta: verbose_name = "收货地址" verbose_name_plural = verbose_name def __str__(self): return self.address
模型编写完成,设置一下我们的数据库配置,我这里使用mysql
myproj/settings.py
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'myproj', 'USER':'root', 'PASSWORD':'123456', 'HOST':'localhost', 'POSRT':'3306', 'OPTIONS': { 'init_command': 'SET default_storage_engine=INNODB', } } }
AUTH_USER_MODEL = 'users.UserProfile' #由于我们在users/models.py继承了django的AbstractUser,所以需要在settings.py中指定我们自定义的user模型,否则创建模型会报E304错误
执行以下命令,生成我们的数据库迁移脚本并生成数据库模型
python manage.py makemigrations
python mange.py migrate
如果报错,显示以下错误信息
因为我们未安装数据库连接驱动
pip3 install -i https://pypi.douban.com/simple/ mysqlclient
编辑myproj/__init__.py
import pymysql
pymysql.install_as_MySQLdb() #因为mysqldb不支持3.5及以上版本,这里我们使用pymysql,这句话意思是将pymysql包传入mysqldb进行初始化
此时仍然报错,因为我们在创建goods/models.py里面的模型时候,使用了DjangoUeditorapp,但我们实际上还未导入这个包,因为这个属于第三方包,我们使用源码导入,并将其放置在extra_apps目录下,源码下载路径:http://ueditor.baidu.com/website/
在myproj/settings.py中注册此app
此时再执行数据库迁移命令,如下
此时,数据库迁移模型已经生成,我们只需再执行migrate命令同步数据库即可
此时数据库中已经有我们的数据表模型了
以上步骤都完成以后就可以开始着手创建系统管理后台了,关于django自带的admin这里不再阐述,我们直接开始xadmin的集成
xadmin的集成
导入xadmin,这里我们选择源码导入,https://github.com/sshwsfc/xadmin/tree/django2下载django2对应的源码到本地,将xadmin文件夹整体拷贝到我们的extra_apps目录下,并在settings.py文件中注册
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'users', #注册app 'goods', 'trade', 'user_operation', 'DjangoUeditor', 'xadmin' #注册xadmin ]
根据xadmin源码,由于xadmin模块需要依赖其它包,我们在这里一一安装
pip3 install -i https://pypi.douban.com/simple/ django-crispy-forms>=1.6.0 django-import-export>=0.5.1 django-reversion>=2.0.0 django-formtools==2.0 future==0.15.2 httplib2==0.9.2 six==1.10.0
在settings.py中注册crispy_forms
创建超级管理员
python manage.py createsuperuser
配置xadminurl
import xadmin urlpatterns = [ # url('admin/', admin.site.urls), re_path(r'xadmin/',xadmin.site.urls) ]
配置完之后,需要同步以下数据库,生成xadmin需要的数据库表
此时,运行项目,访问,localhost:8000/xadmin
至此,我们的xadmin后台就初步搭建完成了,下面我们注册我们的各个app项目
在之前apps下面的app中分别创建adminx.py文件,里面用来定义xadmin的定制类
users/adminx.py
#!/usr/bin/env python # encoding: utf-8 import xadmin from xadmin import views from .models import VerifyCode class BaseSetting(object): enable_themes = True use_bootswatch = True class GlobalSettings(object): site_title = "后台管理" #设置头标题 site_footer = "shopping center" #设置脚标题 # menu_style = "accordion" class VerifyCodeAdmin(object): list_display = ['code', 'mobile', "add_time"] xadmin.site.register(VerifyCode, VerifyCodeAdmin) xadmin.site.register(views.BaseAdminView, BaseSetting) xadmin.site.register(views.CommAdminView, GlobalSettings)
goos/adminx.py
#!/usr/bin/env python # encoding: utf-8 import xadmin from .models import Goods, GoodsCategory, GoodsImage, GoodsCategoryBrand, Banner, HotSearchWords from .models import IndexAd 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_editable = ["is_hot", ] 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"} class GoodsImagesInline(object): model = GoodsImage exclude = ["add_time"] extra = 1 style = 'tab' inlines = [GoodsImagesInline] class GoodsCategoryAdmin(object): list_display = ["name", "category_type", "parent_category", "add_time"] list_filter = ["category_type", "parent_category", "name"] search_fields = ['name', ] 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 class BannerGoodsAdmin(object): list_display = ["goods", "image", "index"] class HotSearchAdmin(object): list_display = ["keywords", "index", "add_time"] class IndexAdAdmin(object): list_display = ["category", "goods"] xadmin.site.register(Goods, GoodsAdmin) xadmin.site.register(GoodsCategory, GoodsCategoryAdmin) xadmin.site.register(Banner, BannerGoodsAdmin) xadmin.site.register(GoodsCategoryBrand, GoodsBrandAdmin) xadmin.site.register(HotSearchWords, HotSearchAdmin) xadmin.site.register(IndexAd, IndexAdAdmin)
trade/adminx.py
# -*- coding: utf-8 -*- __author__ = 'bobby' import xadmin from .models import ShoppingCart, OrderInfo, OrderGoods class ShoppingCartAdmin(object): list_display = ["user", "goods", "nums", ] class OrderInfoAdmin(object): list_display = ["user", "order_sn", "trade_no", "pay_status", "post_script", "order_mount", "order_mount", "pay_time", "add_time"] class OrderGoodsInline(object): model = OrderGoods exclude = ['add_time', ] extra = 1 style = 'tab' inlines = [OrderGoodsInline, ] xadmin.site.register(ShoppingCart, ShoppingCartAdmin) xadmin.site.register(OrderInfo, OrderInfoAdmin)
user_operation/adminx.py
#!/usr/bin/env python # encoding: utf-8 import xadmin from .models import UserFav, UserLeavingMessage, UserAddress class UserFavAdmin(object): list_display = ['user', 'goods', "add_time"] class UserLeavingMessageAdmin(object): list_display = ['user', 'message_type', "message", "add_time"] class UserAddressAdmin(object): list_display = ["signer_name", "signer_mobile", "district", "address"] xadmin.site.register(UserFav, UserFavAdmin) xadmin.site.register(UserAddress, UserAddressAdmin) xadmin.site.register(UserLeavingMessage, UserLeavingMessageAdmin)
到此,我们的项目app都已经集成到xadmin后台中,至于xadmin自定义字段表示的意思,可以查询相关文档