模型类
from django.db import models
# Create your models here.
# 商品类别表:
# cate_name varchar(20)
class Category(models.Model):
cate_name = models.CharField("类别名称", max_length=20)
def __str__(self):
return self.cate_name
class Meta:
db_table = "category"
verbose_name_plural = "商品类别"
# 商品表
# sku_name:商品名字 varchar(100)
# price:价格 13,2
# selling_price:商品销售价格 13,2
# img:图片存放
class Goods(models.Model):
sku_name = models.CharField("商品名字", max_length=100)
price = models.DecimalField("价格", max_digits=13, decimal_places=2)
selling_price = models.DecimalField("商品销售价格", max_digits=13, decimal_places=2)
img = models.CharField("商品默认图片", max_length=200)
title = models.CharField("商品标题", max_length=30)
instruction = models.TextField("商品描述信息", default="")
count = models.IntegerField("商品销量数量", default=0)
stock = models.IntegerField("商品库存数量", default=0)
cate = models.ForeignKey(to=Category, on_delete=models.CASCADE, verbose_name="商品类别")
online = models.BooleanField("是否在售", default=True)
def __str__(self):
return self.sku_name
class Meta:
db_table = "goods"
verbose_name_plural = "商品信息"
# 商品图片
class GoodImg(models.Model):
img = models.CharField("图片地址", max_length=200)
title = models.CharField("标题", max_length=20, null=True, blank=True)
good = models.ForeignKey(to=Goods, on_delete=models.CASCADE, verbose_name="商品")
def __str__(self):
return "%s:%s" % (self.good.sku_name, self.img)
class Meta:
db_table = "goodimg"
verbose_name_plural = "商品图片"
# 轮播图模型类
class Carousel(models.Model):
imgPath = models.CharField("轮播图", max_length=200, null=False)
describes = models.CharField("描述信息", max_length=200, null=False)
def __str__(self):
return "轮播图%s" % self.id
class Meta:
db_table = "carousel"
verbose_name_plural = "轮播图"
配置路由
from django.urls import path
from goods import views
urlpatterns = [
# 加载轮播图
path("carousel/", views.CarouselAPIView.as_view()),
# 加载一类商品
path("oneCategory/all/", views.OneCategoryAPIView.as_view()),
# 加载热门商品
path("hotProduct/", views.HotProductAPIView.as_view()),
# 商品详情
path("detail/", views.GoodDetailAPIView.as_view()),
# 商品详情轮播图
path("detail/goodImg/", views.GoodDetailCarouselAPIView.as_view()),
# 添加历史记录
path("user/history/",views.HistoryAPIView.as_view())
]
模型视图
相关导包
import jwt
import redis
from django.conf import settings
from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from goods.models import *
from functools import reduce
轮播图视图