我的商城开发(第三天)—设计models

一、设计models

1.1 用户表设计:users/models.py

from django.db import models
from django.contrib.auth.models import AbstractUser
from datetime import datetime


# Create your models here.

class UserProfile(AbstractUser):
    """
    用户表,新增字段如下
    """
    GENDER_CHOICE = (
        ('male', '男'),
        ('female', '女')
    )

    # 用户注册时,我们要新建User_Profile 但是我们只有手机号
    name = models.CharField(max_length=30, null=True, blank=True, verbose_name='姓名')
    # 保存出生日期,年龄通过出生日期推算
    birthday = models.DateField(null=True, blank=True, verbose_name='出生年月')
    # 性别
    gender = models.CharField(max_length=6, choices=GENDER_CHOICE, default='female', null=True, blank=True,
                              verbose_name='性别')
    # 手机
    mobile = models.CharField(max_length=11, null=True, blank=True, verbose_name='手机')
    email = models.EmailField(max_length=100, null=True, blank=True, verbose_name='邮箱')
    parent = models.ForeignKey('self', null=True, blank=True, verbose_name='上级', help_text='上级',
                               on_delete=models.SET_NULL)
    is_partner = models.BooleanField(default=False,verbose_name='是否会员', help_text='是否会员')

    class Meta:
        verbose_name = '用户'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.username


class VerifyCode(models.Model):
    """
    验证码
    """
    code = models.CharField(max_length=10, verbose_name="验证码")
    mobile = models.CharField(max_length=11, verbose_name="手机")
    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")

    class Meta:
        verbose_name = "短信验证码"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.code


class UserAddress(models.Model):
    """
    用户收货地址
    """
    user = models.ForeignKey(UserProfile, verbose_name='用户', on_delete=models.CASCADE)
    province = models.CharField(max_length=100, default='', verbose_name='省份')
    city = models.CharField(max_length=100, default='', verbose_name='城市')
    district = models.CharField(max_length=100, default='', verbose_name='区域')
    address = models.CharField(max_length=100, default='', verbose_name='详细地址')
    signer_name = models.CharField(max_length=100, default='', verbose_name='收货人')
    signer_mobile = models.CharField(max_length=11, default='', verbose_name='电话')
    add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')

    class Meta:
        verbose_name = '用户收货地址'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.address


class Partner(models.Model):
    """
    会员申请
    """
    OFFER_STATUS = (
        (1, '申请审核中'),
        (2, '审核通过'),
        (3, '审核拒绝'),
    )
    user = models.ForeignKey(UserProfile, verbose_name='用户', on_delete=models.CASCADE)
    real_name = models.CharField(max_length=30, verbose_name='真实姓名')
    card_bank = models.CharField(max_length=50, verbose_name='银行卡归属银行')
    card_no = models.CharField(max_length=30, verbose_name='银行卡号')
    offer_status= models.IntegerField(default=1, choices=OFFER_STATUS, verbose_name='会员申请状态',
                                           help_text='1 (申请审核中), 2(审核通过),3(审核拒绝)')
    add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')

    class Meta:
        verbose_name = '会员申请'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.real_name

要想替换系统的用户,还要在工程中的settings.py设置:

#重载系统的用户,让UserProfile生效
AUTH_USER_MODEL = 'users.UserProfile'

1.2 商品表设计:goods/models.py

from django.db import models
from datetime import datetime
from DjangoUeditor.models import UEditorField


# Create your models here.

class GoodsCategory(models.Model):
    """
    商品多级分类
    """
    CATEGORY_TYPE = (
        (1, '一级目录'),
        (2, '二级目录'),
        (3, '三级目录')
    )

    # 类别名
    name = models.CharField(default='', max_length=30, verbose_name='类别名', help_text='类别名')
    # 类别编码
    code = models.CharField(default='', max_length=30, verbose_name='类别编码', help_text='类别编码')
    # 类别描述
    desc = models.TextField(default='', max_length=30, verbose_name='类别描述', help_text='类别描述')

    # 设置目录树的级别
    category_type = models.IntegerField(choices=CATEGORY_TYPE, verbose_name='类目录级别', help_text='类目录级别')
    parent_category = models.ForeignKey('self', null=True, blank=True, verbose_name='父类目录级别', help_text='父类目录级别',
                                        on_delete=models.SET_NULL)

    is_tab = models.BooleanField(default=False, verbose_name='是否导航', help_text='是否导航')
    discount = models.IntegerField(default=100, verbose_name='折扣')
    add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')

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

    def __str__(self):
        return self.name


# upload_to=可调用对象
def goods_directory_path(instance, filename):
    # file will be uploaded to MEDIA_ROOT/user_<id>/<filename>
    return 'goods/{0}/{1}'.format(instance.goods_sn, filename)


class Goods(models.Model):
    """
    商品表
    """

    category = models.ForeignKey(GoodsCategory, verbose_name='商品分类', on_delete=models.CASCADE)
    goods_sn = models.CharField(max_length=50, default='', verbose_name='商品唯一货号')
    name = models.CharField(max_length=100, verbose_name='商品名')
    click_num = models.IntegerField(default=0, verbose_name='点击数')
    fav_num = models.IntegerField(default=0, verbose_name='收藏数')
    sold_num = models.IntegerField(default=0, verbose_name='商品销量')
    stocks_num = models.IntegerField(default=0, verbose_name='库存量')
    cost_price = models.FloatField(default=0, verbose_name="成本价格")
    partner_price = models.FloatField(default=0, verbose_name='会员价格')
    market_price = models.FloatField(default=0, verbose_name='市场价格')
    goods_intro = models.TextField(max_length=500, verbose_name='商品简短描述')
    goods_desc = UEditorField(verbose_name='商品内容介绍', imagePath=goods_directory_path, width=1000, height=300,
                              filePath=goods_directory_path, default='')
    goods_show_image = models.ImageField(upload_to=goods_directory_path, null=True, blank=True,
                                         verbose_name='商品海报分享图')
    good_front_images = models.ImageField(upload_to=goods_directory_path, null=True, blank=True,
                                          verbose_name='商品首页图')
    is_shipfee = models.BooleanField(default=True, verbose_name='是否包邮')
    is_new = models.BooleanField(default=False, verbose_name='是否新品')
    is_hot = models.BooleanField(default=False, verbose_name='是否热销')
    is_exchange = models.BooleanField(default=True, verbose_name='是否可退换')
    is_grounding = models.BooleanField(default=True, verbose_name='上下架状态')
    discount = models.IntegerField(default=100, verbose_name='折扣')
    add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')

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

    def __str__(self):
        return self.name


class GoodsImages(models.Model):
    """
    商品详情页轮播图
    """
    goods = models.ForeignKey(Goods, verbose_name='商品', related_name='images', on_delete=models.CASCADE)
    image = models.ImageField(upload_to=goods_directory_path, verbose_name='图片', null=True, blank=True)
    index = models.IntegerField(default=0, verbose_name='轮播顺序')
    add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')

    class Meta:
        verbose_name = '商品详情轮播页'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.goods.name


class Banner(models.Model):
    """
    首页轮播图
    """
    goods = models.ForeignKey(Goods, verbose_name='商品', on_delete=models.CASCADE)
    image = models.ImageField(upload_to='banner', verbose_name='轮播图片')
    index = models.IntegerField(default=0, verbose_name='轮播顺序')
    add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')

    class Meta:
        verbose_name = '首页轮播'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.goods.name

class HotSearchWords(models.Model):
    """
    热搜词
    """
    keywords = models.CharField(default='', max_length=20, verbose_name='热搜词')
    index = models.IntegerField(default=0, verbose_name='排序')
    add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')

    class Meta:
        verbose_name = '热搜词'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.keywords

1.3 交易表设计:trade/models.py

from django.db import models
from datetime import datetime
from users.models import UserAddress
from goods.models import Goods

# get_user_model方法会去setting中找AUTH_USER_MODEL
from django.contrib.auth import get_user_model

User = get_user_model()


# Create your models here.

class ShoppongCart(models.Model):
    """
    购物车
    """
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户')
    goods = models.ForeignKey(Goods, verbose_name='商品', on_delete=models.CASCADE)
    nums = models.IntegerField(default=0, verbose_name='购买数量')

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

    def __str__(self):
        return '%s(%d)'.format(self.goods.name, self.nums)


class OrderStatus(models.Model):
    """
    订单状态:
    0000-新建订单;0001-取消订单;0002-订单已完成;0003-订单结束;
    1000-订单待支付;1001-订单已支付;1002-订单取消支付;
    2000-订单待发货;2001-订单已发货;2002-订单待收货;
    3000-退货退款申请;3001-退货退款审核通过;3002-退货退款审核拒绝;
    """
    order_status = models.CharField(max_length=2, verbose_name='订单状态')
    add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')

    class Meta:
        verbose_name = '订单状态'
        verbose_name_plural = verbose_name

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


class ExpressType(models.Model):
    """
    快递渠道
    """
    express_type = models.CharField(max_length=20, verbose_name='快递渠道')
    add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')

    class Meta:
        verbose_name = '快递渠道'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.express_type


class OrderInfo(models.Model):
    """
    订单信息
    """
    PAY_TYPE = (
        ('alipay', '支付宝'),
        ('wechar', '微信支付'),
    )

    SHIP_TYPE = (
        (1, '国内快递'),
        (2, '同城自取')
    )

    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户')
    order_sn = models.CharField(max_length=30, null=True, blank=True, unique=True, verbose_name='订单号')
    nonce_str = models.CharField(max_length=50, null=True, blank=True, unique=True, verbose_name='随机加密串')
    trade_no = models.CharField(max_length=100, unique=True, null=True, blank=True, verbose_name='第三方交易号')
    order_status = models.ForeignKey(OrderStatus, null=True, blank=True, on_delete=models.SET_NULL, verbose_name='订单状态')
    pay_type = models.CharField(choices=PAY_TYPE, default='alipay', max_length=10, verbose_name='支付通道')
    post_script = models.CharField(max_length=200, verbose_name='订单留言')
    order_count = models.FloatField(default=0.0, verbose_name='订单总金额')
    ship_type = models.IntegerField(choices=SHIP_TYPE, default=2, verbose_name='快递类型')
    ship_count = models.FloatField(default=0.0, verbose_name='快递费用')
    pay_time = models.DateTimeField(null=True, blank=True, verbose_name='支付时间')
    useraddress = models.ForeignKey(UserAddress, null=True, blank=True, on_delete=models.SET_NULL,
                                    verbose_name='用户收货地址')
    expresstype = models.ForeignKey(ExpressType, null=True, blank=True, on_delete=models.SET_NULL,
                                    verbose_name='快递渠道')
    add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')

    class Meta:
        verbose_name = '订单基本信息'
        verbose_name_plural = verbose_name

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


class OrderGoods(models.Model):
    """
    订单内商品详情
    """
    order = models.ForeignKey(OrderInfo, verbose_name='订单信息', related_name='goods', on_delete=models.CASCADE)
    goods = models.ForeignKey(Goods, verbose_name='商品', null=True, blank=True, on_delete=models.SET_NULL)
    goods_num = models.IntegerField(default=0, verbose_name='商品数量')
    add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')

    class Meta:
        verbose_name = '订单内商品项'
        verbose_name_plural = verbose_name

    def __str__(self):
        return str(self.order.order_sn)

1.4 用户操作表设计:user_operation/models.py

from django.db import models
from users.models import UserProfile
from goods.models import Goods
from datetime import datetime
from trade.models import OrderInfo
# get_user_model方法会去setting中找AUTH_USER_MODEL
from django.contrib.auth import get_user_model
User = get_user_model()


# Create your models here.
class UserFav(models.Model):
    """
    用户收藏操作
    """
    user = models.ForeignKey(User, verbose_name='用户', on_delete=models.CASCADE)
    goods = models.ForeignKey(Goods, verbose_name='商品', on_delete=models.CASCADE, help_text='商品ID')
    add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')

    class Meta:
        verbose_name = '用户收藏'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.user.username


# upload_to=可调用对象
def sc_directory_path(instance, filename):
    # file will be uploaded to MEDIA_ROOT/user_<id>/<filename>
    return 'sc/{0}/{1}'.format(instance.workorder_sn, filename)


class UserSaleCounseling(models.Model):
    """
    用户销售咨询操作:售前、售后等
    """
    SC_TYPE = (
        (1, '售后'),
        (2, '投诉'),
        (3, '咨询'),
        (4, '留言'),
        (5, '会员')

    )
    WORKORDER_STATUS = (
        (1, '工单已创建'),
        (2, '工单已完成'),
        (3, '工单已取消'),
    )

    user = models.ForeignKey(User, verbose_name='用户', on_delete=models.CASCADE)
    workorder_sn = models.CharField(max_length=30, null=True, blank=True, unique=True, verbose_name='用户咨询工单号')
    nonce_str = models.CharField(max_length=50, null=True, blank=True, unique=True, verbose_name='随机加密串')
    order = models.ForeignKey(OrderInfo, verbose_name='订单信息', null=True, blank=True, on_delete=models.SET_NULL)
    salecounseling_type = models.IntegerField(default=1, choices=SC_TYPE, verbose_name='咨询类型',
                                              help_text='咨询类型:1(售后), 2(投诉),3(咨询),4(留言)')
    subject = models.CharField(max_length=100, default='', verbose_name='主题')
    content = models.TextField(default='', verbose_name='留言内容', help_text='留言内容')
    images = models.FileField(upload_to="message/images/", verbose_name="上传的图片", help_text="上传的图片")
    workorder_status = models.IntegerField(default=1, choices=WORKORDER_STATUS, verbose_name='工单状态',
                                           help_text='1 (工单已创建), 2(工单已完成),3(工单已取消)')
    add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')

    class Meta:
        verbose_name = '用户咨询'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.workorder_sn

二、应用model

1、makemigrations

~/Desktop/pythonsyscode/Higoleme/Higolemebackend(master*) » python manage.py makemigrations 
Migrations for 'goods':
  apps/goods/migrations/0001_initial.py
    - Create model Banner
    - Create model Goods
    - Create model GoodsCategory
    - Create model GoodsImages
    - Create model HotSearchWords
    - Add field category to goods
    - Add field goods to banner
Migrations for 'trade':
  apps/trade/migrations/0001_initial.py
    - Create model ExpressType
    - Create model OrderGoods
    - Create model OrderInfo
    - Create model OrderStatus
    - Create model ShoppongCart
  apps/trade/migrations/0002_auto_20180925_1003.py
    - Add field user to shoppongcart
    - Add field expresstype to orderinfo
    - Add field order_status to orderinfo
    - Add field user to orderinfo
    - Add field useraddress to orderinfo
    - Add field goods to ordergoods
    - Add field order to ordergoods
Migrations for 'user_operation':
  apps/user_operation/migrations/0001_initial.py
    - Create model UserFav
    - Create model UserSaleCounseling
  apps/user_operation/migrations/0002_auto_20180925_1003.py
    - Add field user to usersalecounseling
    - Add field goods to userfav
    - Add field user to userfav
Migrations for 'users':
  apps/users/migrations/0001_initial.py
    - Create model UserProfile
    - Create model Partner
    - Create model UserAddress
    - Create model VerifyCode

2、migrate

~/Desktop/pythonsyscode/Higoleme/Higolemebackend(master*) » python manage.py migrate       
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, goods, sessions, trade, user_operation, users, xadmin
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0001_initial... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying users.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying goods.0001_initial... OK
  Applying sessions.0001_initial... OK
  Applying trade.0001_initial... OK
  Applying trade.0002_auto_20180925_1003... OK
  Applying user_operation.0001_initial... OK
  Applying user_operation.0002_auto_20180925_1003... OK
  Applying xadmin.0001_initial... OK
  Applying xadmin.0002_log... OK
  Applying xadmin.0003_auto_20160715_0100... OK

三、表模型设计说明

1、创建用户

~/Desktop/pythonsyscode/Higoleme/Higolemebackend(master*) » python manage.py createsuperuser  
Username: admin
邮箱: code4fs@gmail.com
Password: 
Password (again): 
Superuser created successfully.

2、表说明:


评论(1 ) 点赞(20)

  • S 回复
    你好请问可以分享源码吗,前端和后端的

  • 暂未登录,请登录之后发表评论。 QQ