03-01 项目准备
______egon新书python全套来袭请看:https://egonlin.com/book.html
后端框架:django-restframwork
小程序: 创建一个全新的小程序,appid用上面我们注册的正式的appid
小程序的静态文件:在项目目录中创建images文件夹存放小程序重要用到的图片
models.py数据表
后端所有数据表
from django.db import models
import django.utils.timezone as timezone
# Create your models here.
#用户表,没有用django的用户表
class Wxuser(models.Model):
#主键
id = models.AutoField(primary_key=True)
#用户微信唯一标识
openid=models.CharField(max_length=255)
#姓名
name = models.CharField(max_length=50)
#头像
avatar = models.CharField(max_length=200)
#语言
language = models.CharField(max_length=50)
#国家,城市,省份
province = models.CharField(max_length=50)
city = models.CharField(max_length=50)
country = models.CharField(max_length=50)
#性别
gender = models.CharField(max_length=50)
#creat_time=models.DateField(auto_now_add=True,default=timezone.now)
#商品分类表
class Category(models.Model):
#分类id
cat_id=models.AutoField(primary_key=True)
#分类名称
category_name=models.CharField(max_length=50)
#分类的序号
p_order=models.IntegerField(default=0)
#creat_time = models.DateField(auto_now_add=True,default=timezone.now)
#商品表
class Products(models.Model):
#商品id
product_id=models.AutoField(primary_key=True)
#货号
bn=models.CharField(max_length=200)
#条码
barcode=models.CharField(max_length=200)
#名称
name=models.CharField(max_length=200)
#价格
price=models.DecimalField(max_digits=5, decimal_places=2)
#单位重量
weight=models.CharField(max_length=200)
#单位
unit=models.CharField(max_length=200)
#商品分类
cat_id = models.ForeignKey(to="Category",to_field="cat_id", related_name="Products",db_constraint=False,on_delete=models.CASCADE)
#详情介绍
intor = models.CharField(max_length=250)
#商品简介
brief = models.CharField(max_length=250)
#商品图片
image=models.OneToOneField(to='Images',to_field='image_id',on_delete=models.CASCADE,db_constraint=False,default="")
#默认图片
image_default_id = models.CharField(max_length=250)
#creat_time = models.DateField(auto_now_add=True,default=timezone.now)
#订单表
class Order(models.Model):
#订单id
order_id=models.CharField(max_length=250,unique=True,primary_key=True)
#订单的创建时间
createtime=models.DateField(auto_now_add=True)
#订单状态
status_choices = (("active", '活动订单'), ("dead", '作废订单'), ("finish", '已完成订单'))
status = models.CharField(choices=status_choices, default="active",max_length=50)
#订单支付状态
pay_status_choices=((0, '未付款'), (1, '已付款'))
pay_status = models.SmallIntegerField(choices=pay_status_choices, default=0)
#订单支付金额
payed=models.DecimalField(max_digits=5, decimal_places=2)
#订单已支付金额
order_total=models.DecimalField(max_digits=5, decimal_places=2)
#订单发货状态
ship_status_choices=((0, '未发货'), (1, '已发货'),(2, '部分发货'))
ship_status = models.SmallIntegerField(choices=ship_status_choices, default=0)
#支付方式
pay_app =models.CharField(max_length=100)
#购买者会员id
member_id=models.IntegerField(default=0)
#开奖方式
get_method=models.CharField(max_length=100)
#订单包含商品数量
quantity=models.IntegerField(default=0)
#订单祝福语
memo=models.CharField(max_length=200,null=True)
#creat_time = models.DateTimeField(auto_now_add=True)
#订单子订单
class Order_items(models.Model):
#item_id
item_id = models.AutoField(primary_key=True)
#订单id
order_id = models.ForeignKey(to="Order", to_field="order_id", related_name="Order_items", db_constraint=False,on_delete=models.CASCADE)
#商品id
product_id= models.IntegerField(default=0)
#货号
bn = models.CharField(max_length=200)
#条码
barcode = models.CharField(max_length=200)
#商品名称
name = models.CharField(max_length=200)
#商品图片
image = models.OneToOneField(to='Images', to_field='image_id', on_delete=models.CASCADE,db_constraint=False,default="")
#商品价格
price = models.DecimalField(max_digits=5, decimal_places=2)
#明细商品总额(成交价x数量)
amount=models.DecimalField(max_digits=5, decimal_places=2)
#明细商品购买数量
nums=models.IntegerField()
#明细商品发货数量
send_nums=models.IntegerField(null=True)
#商品简介
brief=models.CharField(max_length=250)
#获得者会员用户名
get_mermber_id=models.ForeignKey(to="Wxuser",to_field="id",related_name="Order_items_get",db_constraint=False,on_delete=models.CASCADE,null=True)
#购买者者会员用户名
buy_member_id = models.ForeignKey(to="Wxuser", to_field="id", related_name="Order_items_buy", db_constraint=False,on_delete=models.CASCADE,null=True)
#收货人
consignee_name=models.CharField(max_length=200,null=True)
#收货地区
consignee_area=models.CharField(max_length=200,null=True)
#收货地址
consignee_address=models.CharField(max_length=200,null=True)
#收货地邮编
consignee_zip=models.CharField(max_length=200,null=True)
#收货人手机
consignee_mobile=models.CharField(max_length=200)
#是否填写过收货地址
status_choices = (("send", '已填写'), ("nosend", '未填写'))
status = models.CharField(choices=status_choices, default="nosend",max_length=50)
#creat_time = models.DateTimeField(auto_now_add=True)
class Images(models.Model):
#主键
image_id=models.CharField(max_length=200,primary_key=True)
#存储引擎
storage=models.CharField(max_length=200)
#图片名称
image_name=models.CharField(max_length=250)
#原始图唯一标识
ident=models.CharField(max_length=250)
#原始图URL地址
url=models.CharField(max_length=250)
#大图唯一标识
l_ident=models.CharField(max_length=250)
#大图URL地址
l_url = models.CharField(max_length=250)
#中图唯一标识
m_ident = models.CharField(max_length=250)
#中图URL地址
m_url = models.CharField(max_length=250)
#小图唯一标识
s_ident = models.CharField(max_length=250)
#小图URL地址
s_url = models.CharField(max_length=250)
#迷你图唯一标识
xs_ident = models.CharField(max_length=250)
#迷你图URL地址
xs_url = models.CharField(max_length=250)
#宽度
width=models.CharField(max_length=250)
#高度
height = models.CharField(max_length=250)
status = models.CharField(default="false", max_length=50)
#订单开奖方式表
class Order_methons(models.Model):
#订单id
order=models.OneToOneField(to='Order', to_field='order_id', on_delete=models.CASCADE,db_constraint=False,default="")
#开奖时间
opentime=models.DateTimeField(null=True)
#开奖人数
open_number=models.IntegerField(default=0)
#获奖人id
get_member=models.OneToOneField(to='Wxuser',null=True, to_field='id', on_delete=models.CASCADE,db_constraint=False,default="")
#获得方法
get_method=models.CharField(max_length=50,default="")
#开奖状态
status_choices = (("begin", '未开奖'), ("read", '准备开奖'),('finish',"已经开奖"))
status = models.CharField(choices=status_choices, default="begin", max_length=50)
view_time=models.DateTimeField(null=True)
#会员参与表
class Member_joins(models.Model):
id=models.AutoField(primary_key=True)
#订单id
order=models.OneToOneField(to='Order', to_field='order_id', on_delete=models.CASCADE,db_constraint=False,default="")
#会员id
member=models.ForeignKey(to='Wxuser', to_field='id', on_delete=models.CASCADE,db_constraint=False,default="")
#参与的form_id
form_id=models.CharField(default=0,max_length=250)
#creat_time = models.DateTimeField(auto_now_add=True)
#python3 manage.py makemigrations
#python3 manage.py migrate
#python3 manage.py runserver
序列化组件
MySer.MySer.py
from rest_framework import serializers
from api import models
class ProductsSerializer(serializers.ModelSerializer):
class Meta:
model = models.Products
fields = '__all__'
depth = 1
class CategorySerializer(serializers.ModelSerializer):
class Meta:
model = models.Category
fields = '__all__'
class OrderSerializer(serializers.ModelSerializer):
class Meta:
model = models.Order
fields="__all__"
class OrdermethonsSerializer(serializers.ModelSerializer):
class Meta:
model = models.Order_methons
fields = "__all__"
class OrderitemsSerializer(serializers.ModelSerializer):
class Meta:
model = models.Order_items
fields="__all__"
depth = 1
class WxuserSerializer(serializers.ModelSerializer):
class Meta:
model = models.Wxuser
fields = "__all__"
class MemberJoinSerializer(serializers.ModelSerializer):
class Meta:
model = models.Member_joins
fields = "__all__"
class MemberJoinallSerializer(serializers.ModelSerializer):
class Meta:
model = models.Member_joins
fields = "__all__"
depth=1
MySer.shareSer.py
from rest_framework import serializers
from api import models
class ProductsSerializer1(serializers.ModelSerializer):
class Meta:
model = models.Products
fields = '__all__'
depth = 1
class CategorySerializer1(serializers.ModelSerializer):
class Meta:
model = models.Category
fields = '__all__'
class OrderSerializer1(serializers.ModelSerializer):
class Meta:
model = models.Order
fields="__all__"
class ImageSerializer(serializers.ModelSerializer):
class Meta:
model =models.Images
fields="__all__"
class OrdermethonsSerializer1(serializers.ModelSerializer):
order_id=serializers.CharField(source='order.order_id')
get_member_id= serializers.SerializerMethodField()
def get_get_member_id(self,obj):
if hasattr(obj.get_member, "id"):
return obj.get_member.id
class Meta:
model = models.Order_methons
fields = "__all__"
class OrderitemsSerializer1(serializers.ModelSerializer):
get_mermber_id_id = serializers.SerializerMethodField()
image_id=serializers.SerializerMethodField()
buy_member_id_id=serializers.SerializerMethodField()
number=serializers.SerializerMethodField()
def get_number(self,obj):
if hasattr(obj,"number"):
return obj.number
def get_get_mermber_id_id(self,obj):
if hasattr(obj.get_mermber_id, "id"):
return obj.get_mermber_id.id
def get_image_id(self,obj):
image_obj=ImageSerializer(obj.image, many=False)
return image_obj.data
def get_buy_member_id_id(self,obj):
return obj.buy_member_id.id
class Meta:
model = models.Order_items
fields="__all__"
class WxuserSerializer1(serializers.ModelSerializer):
class Meta:
model = models.Wxuser
fields = "__all__"
class MemberJoinSerializer1(serializers.ModelSerializer):
order_id = serializers.CharField(source='order.order_id')
member_id = serializers.SerializerMethodField()
def get_member_id(self,obj):
return obj.member.id
class Meta:
model = models.Member_joins
fields = "__all__"