文章目录

  • commodity的models.py文件
  • shopper的models.py文件
  • 数据迁移创建数据表


commodity的models.py文件

from django.db import models


# Create your models here.

class Types(models.Model):
id = models.AutoField(primary_key=True)
firsts = models.CharField('一级类型', max_length=100)
seconds = models.CharField('二级类型', max_length=100)

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

class Meta:
verbose_name = '商品类型'
verbose_name_plural = '商品类型'

class CommodityInfos(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField('商品名称',max_length=100)
sezes = models.CharField('颜色规格',max_length=100)
types = models.CharField('商品类型',max_length=100)
price = models.FloatField('商品价格')
discount = models.FloatField('折后价格')
stock = models.IntegerField('存货数量')
sold = models.IntegerField('已售数量')
likes = models.IntegerField('收藏数量')
created = models.DateField('上架日期',auto_now_add=True)
img = models.FileField('商品住图',upload_to=r'imgs')
details = models.FileField('商品介绍',upload_to=r'details')

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

class Meta:
verbose_name = '商品信息'
verbose_name_plural = '商品信息'

shopper的models.py文件

from django.db import models


# Create your models here.

class CartInfos(models.Model):
id = models.AutoField(primary_key=True)
quantity = models.IntegerField('购买数量')
commodityInfos_id = models.IntegerField('商品ID')
user_id = models.IntegerField('用户ID')

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

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


class OrderInfos(models.Model):
id = models.AutoField(primary_key=True)
price = models.FloatField('订单总价')
created = models.DateField('创建时间', auto_now_add=True)
user_id = models.IntegerField('用户ID')
state = models.CharField('订单状态', max_length=20, choices=(
('待支付', '待支付'),
('已支付', '已支付'),
('发货中', '发货中'),
('已签收', '已签收'),
('退货中', '退货中'),
))

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

class Meta:
verbose_name = 'd订单信息'
verbose_name_plural = 'd订单信息'

数据迁移创建数据表

命令行输入

python manage.py makemigrations
python manage.py migrate

Django3项目 商城从0到1 【三】数据模型搭建_django

Django3项目 商城从0到1 【三】数据模型搭建_数据迁移_02

数据库中出现新的表

Django3项目 商城从0到1 【三】数据模型搭建_python_03