1. 程式人生 > 實用技巧 >商品模組表結構分析

商品模組表結構分析

1.表結構分析

1.1商品模組表結構分析

from django.db import models
from utils.MyBaseModel import Base
class Goods(Base):
GOODS_TYPE = (
('1', 'Vip'),
('2', 'Course')
)
CHANNEL_TYPE = (
('1', '普通'),
('2', '促銷')
)
course = models.ForeignKey('course.Course', on_delete=models.PROTECT)
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 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 course/models.py中新增使用者購買課程

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)

1.3 user/models.py新增VIP表並新增使用者關聯

from django.db import models
from utils.MyBaseModel import Base
# 建立Vip表,並和使用者進行關聯
class Vip(Base):
vip_choise = (
('0', '普通使用者'),
('1', '普通會員'),
('2', '高階會員'),
)
title = models.CharField('vip名稱', max_length=16)
vip_type = models.CharField('Vip種類',choices=vip_choise ,max_length=4)
desc = models.CharField('vip描述',max_length=255)
period = models.IntegerField('有效期', default=365)
class Meta:
db_table = 'tb_vip'
def __str__(self):
return self.title
class User(AbstractUser):
phone = models.CharField('手機號',max_length=20)
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'

2.admin.py中註冊

2.1 goods/admin.py中註冊

from django.contrib import admin
from . import models
# Register your models here.
admin.site.register(models.Goods)
admin.site.register(models.GoodsCourse)
admin.site.register(models.Orders)

2.2 user/admin.py中註冊

from django.contrib import admin
from user import models
admin.site.register(models.User)
admin.site.register(models.Vip)