03.表结构
# 1.用户模块表结构
from django.db import models
from django.contrib.auth.models import AbstractUser
from utils.MyBaseModel import Base
class Vip(Base):
title = models.CharField('vip名称', max_length=16)
level = models.CharField('vip等级', max_length=16)
desc = models.CharField('vip描述', max_length=255)
period = models.IntegerField('有效期', default=365)
class Meta:
db_table = 'tb_vip'
def __str__(self):
return "%s-%s" % (self.title, self.level)
class User(AbstractUser):
phone = models.CharField('手机号', max_length=11)
img = models.ImageField(upload_to='user', null=True)
nick_name = models.CharField('昵称', max_length=20)
address = models.CharField('地址', max_length=255)
vip = models.ForeignKey(Vip, on_delete=models.SET_NULL, default=None, null=True)
vip_expiration = models.DateField('vip到期时间', blank=True, default=None, null=True)
class Meta:
db_table = 'tb_user'
class OauthUser(models.Model):
OAUTHTYPE = (
('1', 'weibo'),
('2', 'weixin'),
)
uid = models.CharField('三方用户id', max_length=64)
user = models.ForeignKey('user.User', on_delete=models.CASCADE)
oauth_type = models.CharField('认证类型', max_length=10, choices=OAUTHTYPE)
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
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
# 2.课程模块表关系
from django.db import models
from utils.MyBaseModel import Base
class CourseType(Base):
title = models.CharField('课程类别', max_length=16)
sequence = models.IntegerField('展示顺序', default=10)
class Meta:
db_table = 'tb_coursetype'
def __str__(self):
return self.title
class CourseTag(Base):
title = models.CharField('课程标签', max_length=16)
sequence = models.IntegerField('展示顺序', default=10)
class Meta:
db_table = 'tb_coursetag'
def __str__(self):
return self.title
class Course(Base):
STATUS = (
('0', '即将上线'),
('1', '已上线'),
('2', '已下线'),
)
title = models.CharField('课程名', max_length=24)
desc = models.CharField('课程描述', max_length=256)
img = models.ImageField('课程logo', upload_to='course', null=True)
course_type = models.ForeignKey(CourseType, verbose_name='课程类型', on_delete=models.SET_NULL, default=None, null=True)
course_tag = models.ManyToManyField(CourseTag, verbose_name='课程标签')
status = models.CharField('课程状态', choices=STATUS, max_length=8, default='1')
follower = models.IntegerField('关注人数', default=0)
learner = models.IntegerField('学习人数', default=0)
class Meta:
db_table = 'tb_course'
def __str__(self):
return self.title
class Chapters(Base):
title = models.CharField('章标题', max_length=24)
serial_num = models.IntegerField('章序号')
course = models.ForeignKey(Course, related_name='chapters', on_delete=models.SET_NULL, null=True)
class Meta:
db_table = 'tb_chapters'
def __str__(self):
return self.title
class Sections(Base):
title = models.CharField('节标题', max_length=24)
serial_num = models.IntegerField('节序号')
chapters = models.ForeignKey(Chapters, related_name='sections', on_delete=models.SET_NULL, null=True)
learn_time = models.IntegerField('学习小时', default=1)
video = models.FileField("上传视频", upload_to="videos/%Y%m%d/", blank=True, max_length=1024)
seq_num = models.IntegerField("序号", default=1)
class Meta:
db_table = 'tb_sections'
def __str__(self):
return self.title
class Follow(Base):
user = models.ForeignKey('user.User', on_delete=models.CASCADE)
course = models.ForeignKey(Course, on_delete=models.CASCADE)
class Meta:
db_table = 'tb_follow'
class Path(Base):
title = models.CharField('路径名', max_length=16)
img = models.ImageField('路径图片', upload_to='path', null=True)
desc = models.CharField('路径描述', max_length=255)
user = models.ManyToManyField('user.User', blank=True)
def course_total(self):
count = 0
for jd in self.jieduan.all():
count = count + jd.courses.count()
return count
class Meta:
db_table = 'tb_path'
def __str__(self):
return self.title
class JieDuan(Base):
title = models.CharField('阶段名', max_length=16)
serial_num = models.IntegerField('阶段序号')
path = models.ForeignKey(Path, related_name='jieduan', on_delete=models.SET_NULL, null=True)
courses = models.ManyToManyField(Course, blank=True)
class Meta:
db_table = 'tb_jieduan'
def __str__(self):
return "%s-第%s阶段-%s" % (self.path.title, self.serial_num, self.title)
class UserCourse(Base):
"""
用户购买的课程
"""
user = models.ForeignKey('user.User', on_delete=models.CASCADE, related_name='paycourse')
course = models.ForeignKey('course.Course', on_delete=models.CASCADE, related_name='payuser')
class Meta:
db_table = 'tb_usercourse'
def __str__(self):
return "用户:%s, 课程:%s" % (self.user.username, self.course.title)
class UserSections(Base):
user = models.ForeignKey('user.User', on_delete=models.PROTECT, related_name='usersections')
course = models.ForeignKey(Course, on_delete=models.PROTECT, related_name='usersections')
section = models.ForeignKey(Sections, on_delete=models.PROTECT, related_name='usersections')
class Meta:
db_table = 'tb_usersections'
def __str__(self):
return "用户:%s, 课程:%s, 小节: %s" % (self.user.username, self.course.title, self.sections.title)
# 评论
class Comment(Base):
course = models.ForeignKey(Course, on_delete=models.CASCADE, related_name='course_comment')
user = models.ForeignKey('user.User', on_delete=models.SET_NULL, null=True, related_name='user_comment')
content = models.CharField(max_length=500)
class Meta:
db_table = "tb_comment"
# 评论
# A: 123
# B:A 234
# 评论的回复
class CommentReply(Base):
comment = models.ForeignKey(Comment, on_delete=models.CASCADE, related_name='reply')
user = models.ForeignKey('user.User', on_delete=models.SET_NULL, null=True, related_name='comment')
to_user = models.ForeignKey('user.User', on_delete=models.SET_NULL, null=True, related_name='comment_to')
# replay_id = models.IntegerField('评论回复的id', default=None, null=True)
# comment_reply = models.ForeignKey('self', on_delete=models.CASCADE, default=None, null=True,
# related_name='commentreply')
content = models.CharField(max_length=500)
class Meta:
db_table = "tb_commentreply"
class CommentSelf(Base):
course = models.ForeignKey(Course, on_delete=models.CASCADE, related_name='course_commentself')
user = models.ForeignKey('user.User', on_delete=models.SET_NULL, null=True, related_name='user_commentself')
content = models.CharField(max_length=500)
father = models.ForeignKey('self', on_delete=models.CASCADE, related_name='children', default=None, null=True)
class Meta:
db_table = "tb_commentself"
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# 3.商品模块
from django.db import models
from utils.MyBaseModel import Base
class Goods(Base):
GOODS_TYPE = (
('1', 'Vip'),
('2', 'Course')
)
CHANNEL_TYPE = (
('1', '普通'),
('2', '促销')
)
goods_type = models.CharField('商品种类', choices=GOODS_TYPE, max_length=8)
product_id = models.CharField('产品id', max_length=8)
title = models.CharField('商品名称', max_length=24)
price = models.DecimalField('商品价格', max_digits=8, decimal_places=2)
channel_type = models.CharField('购买渠道', choices=CHANNEL_TYPE, max_length=8)
period = models.IntegerField('有效期', default=365)
is_launched = models.BooleanField('是否上架', default=True)
class Meta:
db_table = 'tb_goods'
def __str__(self):
return self.title
class GoodsCourse(Base):
PAY_TYPE = (
('1', '普通'),
('2', '促销')
)
course = models.ForeignKey('course.Course', on_delete=models.PROTECT)
title = models.CharField('商品名称', max_length=24)
pay_type = models.CharField('购买渠道', choices=PAY_TYPE, max_length=8)
price = models.DecimalField('商品价格', max_digits=8, decimal_places=2)
period = models.IntegerField('有效期', default=365)
is_launched = models.BooleanField('是否上架', default=True)
class Meta:
db_table = 'tb_goodscourse'
def __str__(self):
return self.title
class Orders(Base):
PAY_METHOD = (
(1, "支付宝"),
)
ORDER_STATUS = (
(1, "待支付"),
(2, "已支付"),
(3, "已取消"),
)
user = models.ForeignKey('user.User', on_delete=models.PROTECT, verbose_name="下单用户")
goods = models.ForeignKey(Goods, on_delete=models.PROTECT)
order_id = models.CharField('订单号', max_length=24)
trade_no = models.CharField('支付宝订单号', max_length=32, null=True) # 28位
pay_time = models.DateTimeField('支付时间', null=True)
pay_method = models.CharField('支付方式', choices=PAY_METHOD, default=1, max_length=8)
status = models.CharField('支付状态', choices=ORDER_STATUS, default=1, max_length=8)
total_amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="商品总金额")
# 优惠券
# 收货地址
# ...
class Meta:
db_table = 'tb_orders'
def __str__(self):
return self.order_id
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
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
上次更新: 2024/3/13 15:35:10