Vue Django Restful framework 前后端

2020-04-05  本文已影响0人  AllenBigBear

直接开始了

1:环境的搭建

需要用的,nodejs, npm(cnpm),djangorestfulframework,mysql等

2:项目结构

在项目里构建一个apps的文件夹,专门用于存放所有的app
这个不同于上次我自己做的项目,各个app都是并列于根目录下,这个专门统一存放了。


项目结构

3 Models建模

3.1 Users
class UserProfile(AbstractUser):

    """
    User
    """
    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=(('male','男'),('femal','女')),default='female',verbose_name='性别')
    mobile = models.CharField(max_length=11,verbose_name='电话')
    email = models.CharField(max_length=100,null=True,blank=True,verbose_name='邮箱')

    class Meta:

        verbose_name = '用户'
        verbose_name_plural = '用户'

    def __str__(self):
        return self.name


class VerifyCode(models.Model):
    """
    SMS verficiation
    """
    code = models.CharField(max_length=10,verbose_name='验证码')
    mobile = models.CharField(max_length=11,verbose_name='电话')

    # 注意datetime.now的用法,没有双括号,不然这个字段生成的时间会是建模的时间
    add_time = models.DateTimeField(default=datetime.now,verbose_name='添加时间')

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

    def __str__(self):
        return self.code
3.2 Goods货物

主要是关于货物种类/主页1级类别的商品品牌/具体货物/商品主页轮播图/主页中间位置商品宽幅大图

from django.db import models
from datetime import datetime

from DjangoUeditor.models import UEditorField
# Create your models here.

class GoodsCategory(models.Model):
    """
    Goods Type,货物种类
    """
    CATEGROY_TYPE = (
        (1, "一级类目"),
        (2, "二级类目"),
        (3, "三级类目"),
    )


    name = models.CharField(max_length=30,default="",verbose_name="类别名",help_text="类别名")
    code = models.CharField(max_length=30,default="",verbose_name="类别code",help_text="类别code")
    desc = models.TextField(default="",verbose_name="类别描述",help_text="类别描述")
    category_type = models.IntegerField(choices=CATEGROY_TYPE,verbose_name="类目级别",)
    parent_category = models.ForeignKey("self",null=True,blank=True,verbose_name="父类别",related_name="sub_cat",
                                        on_delete=models.DO_NOTHING)
    is_tab = models.BooleanField(default=False,verbose_name="是否导航",help_text="是否导航")

    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 GoodCategoryBrand(models.Model):
    """
    The recommended brand for each 1st level type,一级商品目录的品牌
    """
    name = models.CharField(max_length=30,default="",verbose_name="品牌名",help_text="品牌")
    desc = models.TextField(max_length=200,default="",verbose_name="品牌描述",help_text="品牌描述")
    image = models.ImageField(max_length=200,upload_to='brand/images/')
    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 Goods(models.Model):
    """
    商品
    """
    category = models.ForeignKey(GoodsCategory,null=True,blank=True,verbose_name="商品类目",on_delete=models.DO_NOTHING)
    goods_sn = models.CharField(max_length=50,default="",verbose_name="商品唯一货号")
    name = models.CharField(max_length=300,verbose_name="商品名")
    click_num = models.IntegerField(default=0,verbose_name="点击数")
    sold_num = models.IntegerField(default=0,verbose_name="商品销售量")
    fav_num = models.IntegerField(default=0,verbose_name="收藏数")
    goods_num = models.IntegerField(default=0,verbose_name="库存数")
    market_price = models.FloatField(default=0,verbose_name="市场价格")
    shop_price = models.FloatField(default=0,verbose_name="本店价格")
    goods_brief = models.TextField(max_length=500,verbose_name="商品简短描述")
    goods_desc = UEditorField(verbose_name='内容',imagePath="goods/images/",width=1000,height=300,filePath='goods/files/',default='')
    ship_free = models.BooleanField(default=True,verbose_name="是否承担运费")
    goods_front_image = models.ImageField(upload_to='',null=True,blank=True,verbose_name='货物首页照片')
    is_new = models.BooleanField(default=False,verbose_name="是否新品")
    is_hot = models.BooleanField(default=False,verbose_name="是否热销")


class GoodsImage(models.Model):
    """
    商品页面轮播图
    """
    goods = models.ForeignKey(Goods,verbose_name="商品",related_name='images',on_delete=models.DO_NOTHING)
    image = models.ImageField(upload_to='',verbose_name='图片',null=True,blank=True)
    image_url = models.CharField(max_length=300,null=True,blank=True,verbose_name='图片url')
    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.DO_NOTHING)
    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

3.3 Trade交易

这里主要建立购物车,订单信息,订单货物信息等模型

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

from goods.models import Goods
# Create your models here.

User = get_user_model() #利用在settings里面设置的AUTH_USER_MODEL,来获取User的类,get_user_model()内部就是读取settings里面设置

class ShoppingCart(models.Model):
    """
    购物车
    """
    user = models.ForeignKey(User,verbose_name="用户",on_delete=models.DO_NOTHING)
    goods = models.ForeignKey(Goods,verbose_name="商品",on_delete=models.DO_NOTHING)
    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 "%s(%d)".format(self.goods.name,self.goods_num)


class OrderInfo(models.Model):
    """
    订单信息
    """

    ORDER_STATUS = (
        ("success","成功"),
        ("cancel","取消"),
        ("cancel","待支付"),
    )



    user = models.ForeignKey(User, verbose_name="用户",on_delete=models.DO_NOTHING)
    order_sn = models.CharField(max_length=30,verbose_name="订单号")
    trade_no = models.CharField(max_length=100,unique=True,null=True,blank=True,verbose_name="交易单号")
    pay_status = models.CharField(choices=ORDER_STATUS,max_length=10,verbose_name="订单支付状态")
    post_script = models.CharField(max_length=200,verbose_name="订单留言")
    order_amount = models.FloatField(default=0.0,verbose_name="订单金额")
    pay_time = models.DateTimeField(null=True,blank=True,verbose_name="支付时间")

    #用户信息
    address = models.CharField(max_length=100,default="",verbose_name="收货地址")
    sign_name = models.CharField(max_length=20,default="",verbose_name="签收人")
    signer_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 str(self.order_sn)


class OrderGoods(models.Model):
    """
    订单详情
    """

    order = models.ForeignKey(OrderInfo,verbose_name="订单信息",on_delete=models.DO_NOTHING)
    goods = models.ForeignKey(Goods,verbose_name="商品",on_delete=models.DO_NOTHING)
    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)
3.4 用户操作模型
from datetime import datetime
from django.contrib.auth import get_user_model

from goods.models import Goods
# Create your models here.

User = get_user_model() #利用在settings里面设置的AUTH_USER_MODEL,来获取User的类,get_user_model()内部就是读取settings里面设置


class UserFav(models.Model):
    """
    用户收藏商品
    """
    user = models.ForeignKey(User,verbose_name="用户",on_delete=models.DO_NOTHING)
    goods = models.ForeignKey(Goods,verbose_name="商品",on_delete=models.DO_NOTHING)

    add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')

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

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


class UserLeavingMessage(models.Model):
    """
    用户留言
    """

    MESSAGE_CHOICES = (
        (1, "留言"),
        (2, "投诉"),
        (3, "询问"),
        (4, "售后"),
        (5, "求购"),
    )

    user = models.ForeignKey(User,verbose_name="用户",on_delete=models.DO_NOTHING)
    message_type = models.IntegerField(default=1,choices=MESSAGE_CHOICES,verbose_name="留言类型",
                                       help_text="留言类型:1(留言),2(投诉),3(询问),4(售后),5(求购)")
    subject = models.CharField(max_length=100,default="",verbose_name="主题")
    message = models.TextField(default="",verbose_name="留言内容",help_text="留言内容")
    file = models.FileField(verbose_name="上传的文件",help_text="上传的文件")

    add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')

    class Meta:
        verbose_name = "用户留言"
        verbose_name_plural = verbose_name


    def __str__(self):
        return self.subject


class UserAddress(models.Model):
    """
    用户收货地址
    """
    user = models.ForeignKey(User,verbose_name="用户",on_delete=models.DO_NOTHING)
    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="签收人")
    sign_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

4:注意事项

1:Django 2.0以后,所有的外键都必须加上on_delete参数
2:在settings里面设置mysql的时候,OPTION选项设置时要加一个default

DATABASES = {
    # 'default': {
    #     'ENGINE': 'django.db.backends.sqlite3',
    #     'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    # }
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': "mxshop",
        'USER':'root',
        'PASSWORD':',
        'HOST': "127.0.0.1",
        'PORT':'3306',
        'OPTIONS':{'init_command': 'SET default_storage_engine = INNODB;'}
    }
}
上一篇下一篇

猜你喜欢

热点阅读