Vue+Django REST Framework生鲜电商项目学习笔记——model设计和资源导入

分为以下几个环节:

  1. user用户的model设计
  2. goods商品的model设计
  3. trade交易的model设计
  4. user_operation用户操作的model设计
  5. migrations原理及表生成
  6. xadmin后台管理系统的配置
  7. 导入商品和商品类别数据

user用户的model设计

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from datetime import datetime

from django.db import models
from django.contrib.auth.models import AbstractUser


# Create your models here.


class UserProfile(AbstractUser):
"""
用户
"""
name = models.CharField(max_length=30, null=True, blank=True, verbose_name="姓名")
birthday = models.DateField(null=True, blank=True, verbose_name="出生年月")
mobile = models.CharField(max_length=11, verbose_name="电话")
gender = models.CharField(max_length=6, choices=(("male", "男"), ("female", "女")), default="female",
verbose_name="性别")
email = models.CharField(max_length=100, null=True, blank=True, verbose_name="邮箱")

class Meta:
verbose_name = "用户"
verbose_name_plural = verbose_name

def __str__(self):
return self.name


class VerifyCode(models.Model):
"""
短信验证码
"""
code = models.CharField(max_length=10, verbose_name="验证码")
mobile = models.CharField(max_length=11, 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.code

然后在settings文件中添加下面这行代码以替换django本身的用户。

1
AUTH_USER_MODEL = 'users.UserProfile'

goods商品的model设计

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
from datetime import datetime

from django.db import models
from DjangoUeditor.models import UEditorField


# Create your models here.
class GoodsCategory(models.Model):
"""
商品类别
"""

CATEGORY_TYPE = (
(1, "一级类目"),
(2, "二级类目"),
(3, "三级类目"),
)
name = models.CharField(default="", max_length=30, verbose_name="类别名", help_text="类别名")
code = models.CharField(default="", max_length=30, verbose_name="类别code", help_text="类别code")
desc = models.TextField(default="", verbose_name="类别描述", help_text="类别描述")
category_type = models.IntegerField(choices=CATEGORY_TYPE, verbose_name="类目级别", help_text="类目级别")
parent_category = models.ForeignKey("self", null=True, blank=True, verbose_name="父类目级别", help_text="父目录",
related_name="sub_cat")
is_tab = models.BooleanField(default=False, 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.name


class GoodsCategoryBrand(models.Model):
"""
品牌
"""
category = models.ForeignKey(GoodsCategory, related_name='brands', null=True, blank=True, verbose_name="商品类目")
name = models.CharField(default="", max_length=30, verbose_name="品牌名", help_text="品牌名")
desc = models.TextField(default="", max_length=200, verbose_name="品牌描述", help_text="品牌描述")
image = models.ImageField(max_length=200, upload_to="brand/")
add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")

class Meta:
verbose_name = "品牌"
verbose_name_plural = verbose_name

def __str__(self):
return self.name


class Goods(models.Model):
"""
商品
"""
category = models.ForeignKey(GoodsCategory, verbose_name="商品类目")
goods_sn = models.CharField(max_length=50, default="", verbose_name="商品唯一货号")
name = models.CharField(max_length=100, verbose_name="商品名")
click_num = models.IntegerField(default=0, verbose_name="点击数")
sold_num = models.IntegerField(default=0, verbose_name="商品销售量")
fav_num = models.IntegerField(default=0, verbose_name="收藏数")
goods_num = models.IntegerField(default=0, verbose_name="库存数")
market_price = models.FloatField(default=0, verbose_name="市场价格")
shop_price = models.FloatField(default=0, verbose_name="本店价格")
goods_brief = models.TextField(max_length=500, verbose_name="商品简短描述")
goods_desc = UEditorField(verbose_name="内容", imagePath="goods/images/", width=1000, height=300,
filePath="goods/files/", default='')
ship_free = models.BooleanField(default=True, verbose_name="是否承担运费")
goods_front_image = models.ImageField(upload_to="goods/images/", null=True, blank=True, verbose_name="封面图")
is_new = models.BooleanField(default=False, verbose_name="是否新品")
is_hot = models.BooleanField(default=False, 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.name


class GoodsImage(models.Model):
"""
商品轮播图
"""
goods = models.ForeignKey(Goods, verbose_name="商品", related_name="images")
image = models.ImageField(upload_to="", verbose_name="图片", null=True, blank=True)
add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")

class Meta:
verbose_name = "商品轮播图"
verbose_name_plural = verbose_name

def __str__(self):
return self.goods.name


class Banner(models.Model):
"""
轮播的商品
"""
goods = models.ForeignKey(Goods, verbose_name="商品")
image = models.ImageField(upload_to='banner', verbose_name="轮播图片")
index = models.IntegerField(default=0, 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.goods.name

其中需要使用DjangoUeditor,直接解压放到extra_apps目录下即可,记得在settings.py文件中注册我们的app:

1
2
3
4
5
6
7
8
9
10
11
12
13
INSTALLED_APPS = [
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'apps.users.apps.UsersConfig',
'DjangoUeditor',
'users',
'goods',
'trade',
'user_operation'
]

trade交易的model设计

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from datetime import datetime

from django.db import models
from django.contrib.auth import get_user_model()

from goods.models import Goods

User = get_user_model()


# Create your models here.

class ShoppingCart(models.Model):
"""
购物车
"""
user = models.ForeignKey(User, verbose_name="用户")
goods = models.ForeignKey(Goods, verbose_name="商品")
goods_num = models.IntegerField(default=0, verbose_name="购买数量")
add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")

class Meta:
verbose_name = "购物车"
verbose_name_plural = verbose_name

def __str__(self):
return "%s(%d)".format(self.goods.name, self.goods_num)


class OrderInfo(models.Model):
"""
订单
"""
ORDER_STATUS = (
("success", "成功"),
("cancel", "取消"),
("cancel", "待支付"),
)

user = models.ForeignKey(User, verbose_name="用户")
order_sn = models.CharField(max_length=30, null=True, blank=True, unique=True, verbose_name="订单号")
trade_no = models.CharField(max_length=100, unique=True, null=True, blank=True, verbose_name=u"交易号")
pay_status = models.CharField(choices=ORDER_STATUS, default="paying", max_length=30, verbose_name="订单状态")
post_script = models.CharField(max_length=200, verbose_name="订单留言")
order_mount = models.FloatField(default=0.0, verbose_name="订单金额")
pay_time = models.DateTimeField(null=True, blank=True, verbose_name="支付时间")
address = models.CharField(max_length=100, default="", verbose_name="收货地址")
signer_name = models.CharField(max_length=20, default="", verbose_name="签收人")
singer_mobile = models.CharField(max_length=11, verbose_name="联系电话")
add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")

class Meta:
verbose_name = "订单"
verbose_name_plural = verbose_name

def __str__(self):
return str(self.order_sn)


class OrderGoods(models.Model):
"""
订单商品详情
"""
order = models.ForeignKey(OrderInfo, verbose_name="订单信息", related_name="goods")
goods = models.ForeignKey(Goods, verbose_name="商品")
goods_num = models.IntegerField(default=0, verbose_name="商品数量")
add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")

class Meta:
verbose_name = "订单商品"
verbose_name_plural = verbose_name

def __str__(self):
return str(self.order.order_sn)

注:Django 1.5之后y用户可以自定义User Model,可以通过以下两种方式来获取User Model。

  1. 使用django.contrib.auth.get_user_model()
1
2
3
4
5
6
7
8
9
# 使用默认User model时
>>> from django.contrib.auth import get_user_model
>>> get_user_model()
<class 'django.contrib.auth.models.User'>

# 使用自定义User model
>>> from django.contrib.auth import get_user_model
>>> get_user_model()
<class 'xxx.models.UserProfile'>
  1. 使用settings.AUTH_USER_MODEL

应该使用django.contrib.auth.get_user_model()来引用用户模型,而不要直接引用User。 这个方法将返回当前正在使用的用户模型 —— 指定的自定义用户模型或者User。当定义一个外键或者到用户模型的多对多关系时,你应该使用 settings.AUTH_USER_MODEL设置来指定自定义的模型。一般来说,在导入时候执行的代码中,应该使用settings.AUTH_USER_MODEL,get_user_model() 只在Django 已经导入所有的模型后才工作。

user_operation用户操作的model设计

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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="用户")
goods = models.ForeignKey(Goods, verbose_name="商品", help_text="商品id")
add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")

class Meta:
verbose_name = "用户收藏"
verbose_name_plural = verbose_name

def __str__(self):
return self.user.name


class UserLeavingMessage(models.Model):
"""
用户留言
"""
MESSAGE_CHOICES = (
(1, "留言"),
(2, "投诉"),
(3, "询问"),
(4, "售后"),
(5, "求购")
)

user = models.ForeignKey(User, verbose_name="用户")
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="用户")
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

migrations原理及表生成

直接Run manage.py Task然后makemigrations

原理略过,官方文档讲的很详细。

xadmin后台管理系统的配置

xadmin中的xadmin文件夹复制到extra_apps目录下,将其中每个app目录下的adminx.py文件复制到项目对应app目录下。

安装xadmin依赖包:

1
pip install django-crispy-forms django-reversion django-formtools future httplib2 six xlwt xlswriter -i https://pypi.douban.com/simple/

在settings.py文件中添加配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
INSTALLED_APPS = [
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_filters',
'DjangoUeditor',
'apps.users.apps.UsersConfig',
'goods.apps.GoodsConfig',
'trade.apps.TradeConfig',
'user_operation.apps.UserOperationConfig',
'crispy_forms',
'xadmin',
]

# 设置时区
LANGUAGE_CODE = 'zh-hans' # 中文支持,django1.8以后支持;1.8以前是zh-cn
TIME_ZONE = 'Asia/Shanghai'
USE_I18N = True
USE_L10N = True
USE_TZ = False # 默认是Ture,时间是utc时间,由于我们要用本地时间,所用手动修改为false!!!!

再执行makemigrations和migrate

修改Mxshop/urls.py文件:

1
2
3
4
5
6
from django.conf.urls import url
import xadmin

urlpatterns = [
url(r'^xadmin/', xadmin.site.urls),
]

创建超级用户

Debug运行,打开http://127.0.0.1:8000/xadmin/

发现app名称仍是英文,下面进入对应app下的apps.py文件进行修改,例如goods:

1
2
3
4
5
6
from django.apps import AppConfig


class GoodsConfig(AppConfig):
name = 'goods'
verbose_name = "商品"

OK,大功告成!

导入商品和商品类别数据

data中的goods和brands文件夹复制到项目的media文件夹中,并在db_tools文件夹下新建data文件夹,拖入category_data.py和product_data.py文件。

在db_tools文件夹下新建import_category_data.py:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 独立使用django的model
import sys
import os

# 获取当前文件的目录
pwd = os.path.dirname(os.path.realpath(__file__))
# 获取项目名的目录(因为我的当前文件是在项目名下的文件夹下的文件.所以是../)
sys.path.append(pwd + "../")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "MxShop.settings")

import django

django.setup()

from goods.models import GoodsCategory

from db_tools.data.category_data import row_data

for lev1_cat in row_data:
lev1_instance = GoodsCategory()
lev1_instance.code = lev1_cat["code"]
lev1_instance.name = lev1_cat["name"]
lev1_instance.category_type = 1
lev1_instance.save()

for lev2_cat in lev1_cat["sub_categorys"]:
lev2_instance = GoodsCategory()
lev2_instance.code = lev2_cat["code"]
lev2_instance.name = lev2_cat["name"]
lev2_instance.category_type = 2
lev2_instance.parent_category = lev1_instance
lev2_instance.save()

for lev3_cat in lev2_cat["sub_categorys"]:
lev3_instance = GoodsCategory()
lev3_instance.code = lev3_cat["code"]
lev3_instance.name = lev3_cat["name"]
lev3_instance.category_type = 3
lev3_instance.parent_category = lev2_instance
lev3_instance.save()

运行之后即可导入商品类别数据,可以再navicat或者后台管理系统中查看:

接下来导入商品数据,观察商品数据结构之后,在db_tools文件夹下新建import_goods_data.py:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import sys
import os

# 获取当前文件的目录
pwd = os.path.dirname(os.path.realpath(__file__))
# 获取项目名的目录(因为我的当前文件是在项目名下的文件夹下的文件.所以是../)
sys.path.append(pwd + "../")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "MxShop.settings")

import django

django.setup()

from goods.models import Goods, GoodsCategory, GoodsImage

from db_tools.data.product_data import row_data

for goods_detail in row_data:
goods = Goods()
goods.name = goods_detail["name"]
goods.market_price = float(int(goods_detail["market_price"].replace("¥", "").replace("元", "")))
goods.shop_price = float(int(goods_detail["sale_price"].replace("¥", "").replace("元", "")))
goods.goods_brief = goods_detail["desc"] if goods_detail["desc"] is not None else ""
goods.goods_desc = goods_detail["goods_desc"] if goods_detail["goods_desc"] is not None else ""
goods.goods_front_image = goods_detail["images"][0] if goods_detail["images"] else ""

category_name = goods_detail["categorys"][-1]
category = GoodsCategory.objects.filter(name=category_name)

if category:
goods.category = category[0]
goods.save()

for goods_image in goods_detail["images"]:
goods_image_instance = GoodsImage()
goods_image_instance.image = goods_image
goods_image_instance.goods = goods
goods_image_instance.save()

运行即可。

但是发现其中的图片无法显示,需要我们进行配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# settings.py文件中添加
MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, "media")

# MxShop/urls.py文件中修改
from django.conf.urls import url
import xadmin
from MxShop.settings import MEDIA_ROOT
from django.views.static import serve

urlpatterns = [
url(r'^xadmin/', xadmin.site.urls),
url(r'^media/(?P<path>.*)$', serve, {"document_root": MEDIA_ROOT}),
]

Django settings.py的media路径设置

🐶 ~怕是要给老板下跪了哦~ 🐶