3.1 创建用户模型course
python ../manage.py startapp goods # 创建course模型
3.2 在setting.py中注册goods模型(第一步)
INSTALLED_APPS = [ 'goods.apps.GoodsConfig', ] # 注:pycharm无法联想,需要把apps文件夹设置为源根
3.3 syl/urls.py添加路由(第二步)
urlpatterns = [ path('goods/', include('goods.urls')), ]
3.4 创建 goods/urls.py路由文件(第三步)
from django.urls import path, re_path from . import views urlpatterns = [ ]
创建商品表
from django.db import models # Create your models here. from django.db import models from utils.MyBaseModel import Base # class Base(models.Model): # create_time=models.DateTimeField('创建时间',auto_now_add=True,null=True) # update_time=models.DateTimeField('更新时间',auto_now=True,null=True) # # class Meta: # abstract=True class Goods(Base): GOOD_TYPE=( ('1','VIP'), ('2','Course') ) CHANNEL_TYPE=( ('1','普通'), ('2','促销') ) course=models.ForeignKey('course.Course',on_delete=models.PROTECT) goods_type=models.CharField('商品种类',choices=GOOD_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) pay_time=models.DateTimeField('支付时间',null=True) pay_method=models.CharField('支付方式',choices=ORDER_STATUS,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 class GoodsCourse(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)
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.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)