说明:创建数据库之前应该有一个需求分析的过程,这个过程主要是依据需求方的需求文档,进行分析,需要什么功能,实现这些功能需要哪些表,表与表之间的关联如何,以及实现一些功能时需要用到哪些技术,都需要在这个步骤分析清楚。
1、创建基类base_model
在项目目录下创建python文件夹db—>创建python文件base_model.py——>创建基类,代码如下:
from django.db import modelsclass BaseModel(models.Model):"""创建一个数据库的基类,"""create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')modify_time = models.DateTimeField(auto_now=True, verbose_name='修改时间')is_delete = models.BooleanField(default=True, verbose_name='删除标记')class Meta:abstract = True # 说明这是一个抽象类的模型。
2、user应用下创建User表,直接继承django自导的abstractUser类。
from django.contrib.auth.models import AbstractUser
from db.base_model import BaseModel
from django.db import modelsclass User(AbstractUser, BaseModel):"""这里是直接使用django自带的user类"""class Meta:db_table = 'df_user'verbose_name_plural = verbose_name = '用户'class AddressManager(models.Manager):'''地址模型管理器类'''# 1.改变原有查询的结果集:all()# 2.封装方法:用户操作模型类对应的数据表(增删改查)def get_default_address(self, user):'''获取用户默认收货地址'''# self.model:获取self对象所在的模型类try:address = self.get(user=user, is_default=True) # models.Managerexcept self.model.DoesNotExist:# 不存在默认收货地址address = Nonereturn addressclass Address(BaseModel):'''地址模型类'''user = models.ForeignKey('User', verbose_name='所属账户', on_delete=models.CASCADE)receiver = models.CharField(max_length=20, verbose_name='收件人')addr = models.CharField(max_length=256, verbose_name='收件地址')zip_code = models.CharField(max_length=6, null=True, verbose_name='邮政编码')phone = models.CharField(max_length=11, verbose_name='联系电话')is_default = models.BooleanField(default=False, verbose_name='是否默认')# 自定义一个模型管理器对象objects = AddressManager()class Meta:db_table = 'df_address'verbose_name = '地址'verbose_name_plural = verbose_name
3、order应用下创建订单信息表,订单商品表,继承Basemodel,·
from db.base_model import BaseModel
from django.db import models# 这个是订单相关的表:订单信息表,订单的商品表class OrderInfo(BaseModel):"""订单信息表"""PAY_METHODS_CHOICE = ((1, '货到付款'),(2, '微信支付'),(3, '支付宝'),(4, '银联支付'))ORDER_STATUS_CHOICE = ((1, '待支付'),(2, '待发货'),(3, '待收货'),(4, '待评价'),(5, '已完成'))order_id = models.IntegerField(primary_key=True, verbose_name='订单id')address = models.ForeignKey('user.Address', on_delete=models.CASCADE, verbose_name='地址id')user = models.ForeignKey('user.User', on_delete=models.CASCADE, verbose_name='用户id')pay_method = models.CharField(max_length=32, choices=PAY_METHODS_CHOICE, default=1, verbose_name='支付方式')total_count = models.IntegerField(verbose_name='总的数目')total_price = models.DecimalField(max_digits=128, decimal_places=0,verbose_name='总的金额') # max_digits:数字允许的最大位数;decimal_places:小数的最大位数trans_price = models.DecimalField(max_digits=128, decimal_places=0, verbose_name='运费')order_status = models.CharField(max_length=32, choices=ORDER_STATUS_CHOICE, default=1, verbose_name='订单状态')trade_no = models.IntegerField(verbose_name='支付编号')class Meta:db_table = 'df_order_info'verbose_name_plural = verbose_name = '订单'class OrderGoods(BaseModel):order = models.ForeignKey('OrderInfo', verbose_name='订单', on_delete=models.CASCADE)sku = models.ForeignKey('goods.GoodsSKU', verbose_name='商品SKU', on_delete=models.CASCADE)count = models.IntegerField(default=1, verbose_name='商品数目')price = models.DecimalField(max_digits=12, decimal_places=2, verbose_name='商品价格')comment = models.CharField(max_length=256, default='', verbose_name='评论')class Meta:db_table = 'df_order_goods'verbose_name_plural = verbose_name = '订单商品'
4、goods应用下创建商品类型表,商品SKU表,商品SPU表,商品图片表,首页轮播商品表,首页分类商品展示表,首页促销活动表,继承Basemodel数据库基类。
from django.db import models
from tinymce.models import HTMLField
from db.base_model import BaseModelclass GoodsType(BaseModel):"""商品模型类"""name = models.CharField(max_length=32, verbose_name='种类名称')logo = models.CharField(max_length=32, verbose_name='标识')image = models.ImageField(upload_to='type', verbose_name='商品类型图片')class Meta:db_table = 'df_goods_type'verbose_name_plural = verbose_name = '商品种类'class GoodsSKU(BaseModel):"""商品SKU表"""STATUS_CHOICE = ((0, '下线'), (1, '上线'))type = models.ForeignKey('GoodsType', verbose_name='商品种类',on_delete=models.CASCADE)goods = models.ForeignKey('Goods', verbose_name='商品名称',on_delete=models.CASCADE)name = models.CharField(max_length=20, verbose_name='商品名称')desc = models.CharField(max_length=256, verbose_name='商品简介')price = models.DecimalField(max_digits=32,decimal_places=0, verbose_name='商品价格')unite = models.IntegerField(verbose_name='商品单位')image = models.ImageField(upload_to='goods', verbose_name='商品图片')stock = models.IntegerField(verbose_name='商品库存')sales = models.IntegerField(default=0, verbose_name='商品销量')status = models.CharField(max_length=32,default=1, choices=STATUS_CHOICE, verbose_name='商品状态')class Meta:db_table = 'df_goods_sku'verbose_name_plural = verbose_name = '商品'class Goods(BaseModel):"""商品SPU模型"""name = models.CharField(max_length=56, verbose_name='商品名称')detail = HTMLField(blank=True, verbose_name='商品详情')class Meta:db_table = 'df_goods'verbose_name_plural = verbose_name = '商品SKU'class GoodsImage(BaseModel):"""商品图片"""sku = models.ForeignKey('GoodsSKU', verbose_name='商品',on_delete=models.CASCADE)image = models.ImageField(upload_to='goods', verbose_name='图片路径')class Meta:db_table = 'df_goods_image'verbose_name_plural = verbose_name = '商品图片'class IndexGoodsBanner(BaseModel):"""首页轮播商品展示"""sku = models.ForeignKey('GoodsSKU',on_delete=models.CASCADE)image = models.ImageField(upload_to='banner', verbose_name='图片')index = models.SmallIntegerField(default=0, verbose_name='展示顺序')class Meta:db_table = 'df_index_banner'verbose_name_plural = verbose_name = '首页轮播商品'class IndexTypeGoodsBanner(BaseModel):"""首页分类商品展示模型"""DISPLAY_TYPE_CHOICE = ((0, '标题'), (1, '图片'))type = models.ForeignKey('GoodsType', verbose_name='商品类型',on_delete=models.CASCADE)sku = models.ForeignKey('GoodsSKU', verbose_name='商品SKU',on_delete=models.CASCADE)display_type = models.CharField(max_length=56,default=1, choices=DISPLAY_TYPE_CHOICE, verbose_name='展示类型')index = models.SmallIntegerField(default=0, verbose_name='展示顺序') # 1,2,3,4class Meta:db_table = 'df_index_type_goods'verbose_name_plural = verbose_name = '主页分类展示商品'class IndexPromotionBanner(BaseModel):"""首页促销活动模型"""name = models.CharField(max_length=32, verbose_name='活动名称')url = models.URLField(max_length=256, verbose_name='活动链接')image = models.ImageField(upload_to='banner', verbose_name='活动图片')index = models.SmallIntegerField(default=0, verbose_name='展示顺序')class Meta:db_table = 'df_index_promotion'verbose_name_plural = verbose_name = '主页促销活动'
5、cart 应用下,无需创建模型
自此,模型来创建完成
*说明:*这里使用的数据库是sqllite,所以就直接迁移就好,
我运用的事django自带的认证系统,所以在setting.py 配置文件中要加入
AUTH_USER_MODEL = 'user.User'
1、生成模型:python manage.py make migrations
2、迁移指令:python manage.py migrate
迁移成功