本文主要是介绍Vue + Django API 前后端分离学习笔记 3.3 goods的model设计,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
直接上代码
# -*- coding:utf-8 -*-from datetime import datetimefrom django.db import models
from DjangoUeditor.models import UEditorField# Create your models here.
# 这里可以实现用户自定义类别(三级)class GoodsCategory(models.Model):"""商品类别"""CATEGORY_TYPE = ((1, u'一级类目'),(2, u'二级类目'),(3, u'三级类目'))name = models.CharField(default='', max_length=30, verbose_name=u'类别名', help_text=u'类别名')code = models.CharField(default='', max_length=30, verbose_name=u'类别code', help_text=u'类别code' )desc = models.CharField(default='', verbose_name=u'类别描述', help_text=u'类别描述')category_type = models.CharField(choices=CATEGORY_TYPE, verbose_name=u'类目级别', help_text=u'类目级别')
#知识点 递归调用 自撸parent_category = models.ForeignKey("self", null=True, blank=True, verbose_name=u'父类别', help_text=u'父类别',related_name='sub_cat')
# 用与是否要在导航栏显示的商品类别is_tab = models.BooleanField(default=False, verbose_name=u'是否导航', help_text=u'是否导航')add_time = models.DateTimeField(default=datetime.now, verbose_name=u'添加时间')class Meta:verbose_name = u'商品类别'verbose_name_plural = verbose_namedef __str__(self):self.name#用于存储商品品牌信息
class GoodsCategoryBrand(models.Model):"""品牌名"""name = models.CharField(default='', max_length=30, verbose_name=u'品牌名', help_text=u'品牌名')desc = models.TextField(default='', max_length=200, verbose_name=u'品牌描述', help_text=u'品牌描述')image = models.ImageField(upload_to = "brand/images/", max_length=200)add_time = models.DateTimeField(default=datetime.now, verbose_name=u'添加时间')class Meta:verbose_name = u'品牌'verbose_name_plural = verbose_namedef __str__(self):return self.nameclass Goods(models.Model):"""商品"""category = models.ForeignKey(GoodsCategory, null=True, blank=True, verbose_name=u'商品类目', on_delete=models.CASCADE)goods_sn = models.CharField(max_length=50, default='', verbose_name=u'商品唯一货号')name = models.CharField(max_length=300, verbose_name=u'商品名')click_nums = models.IntegerField(default=0, verbose_name=u'点击数')sold_num = models.IntegerField(default=0, verbose_name=u'商品销售量')fav_num = models.IntegerField(default=0, verbose_name=u'收藏数')goods_num = models.IntegerField(default=0, verbose_name=u'库存数')market_price = models.FloatField(default=0, verbose_name=u'市场价格')shop_price = models.FloatField(default=0, verbose_name=u'本店价格')goods_brief = models.TextField(default=0, verbose_name=u'商品简短描述')
# 富文本插件 这个插件直接用教程的现成的(以后真正的环境就自己想办法用别的吧 原版不更新了)goods_desc = UEditorField(verbose_name=u'内容', imagePath='goods/images/', width=1000, height=300,filePath='goods/files/', default='')ship_free = models.BooleanField(default=True, verbose_name=u'是否承担运费')goods_front_image = models.ImageField(upload_to='', null=True, blank=True, verbose_name='封面图')is_new = models.BooleanField(default=False, verbose_name=u'是否新品')is_hot = models.BooleanField(default=False, verbose_name=u'是否热销')add_time = models.DateTimeField(default=datetime.now, verbose_name=u'添加时间')class Meta:verbose_name = u'商品'verbose_name_plural = verbose_namedef __str__(self):return self.name#商品详情的图片(不是首页的轮播图)
class GoodsImage(models.Model):"""商品轮播图"""goods = models.ForeignKey(Goods, verbose_name=u'商品', related_name = 'images', on_delete=models.CASCADE)image = models.ImageField(upload_to='', verbose_name=u'图片', null=True, blank=True)image_url = models.CharField(max_length=300, null=True, blank=True, verbose_name=u'图片url')add_time = models.DateTimeField(default=datetime.now, verbose_name=u'添加时间')class Meta:verbose_name = u'商品图片'verbose_name_plural = verbose_namedef __str__(self):return self.goods.name# 首页轮播图
class Banner(models.Model):"""轮播的商品"""goods = models.ForeignKey(Goods, verbose_name=u'商品', on_delete=models.CASCADE)image = models.ImageField(upload_to='banner', verbose_name=u'轮播图片')index = models.IntegerField(default=0, verbose_name=u'轮播顺序')add_time = models.DateTimeField(default=datetime.now, verbose_name=u'添加时间')class Meta:verbose_name = u'轮播商品'verbose_name_plural = verbose_namedef __str__(self):return self.goods.name
然后 在settings.py中添加app
INSTALLED_APPS = ['django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','users.apps.UsersConfig','DjangoUeditor','users','goods','trade','user_operation'
]
完事~吃饭去
这篇关于Vue + Django API 前后端分离学习笔记 3.3 goods的model设计的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!