广告小程序后端开发(2.Models设计)

1.users的数据表设计:

 1.重建用户表:

1.在settings中配置:

AUTH_USER_MODEL=users.UserProfile

2.apps/users/models.py中建立UserProfile表:

class UserProfile(AbstractUser):
    """
    用户表
    """
    nickName=models.CharField(max_length=30,null=True,blank=True,verbose_name=微信名)
    avatarUrl = models.CharField(max_length=200, null=True, blank=True, verbose_name=用户头像URL)
    open_id = models.CharField(max_length=64, null=True, blank=True, verbose_name=OpenId)
    token = models.CharField(max_length=64, null=True, blank=True, verbose_name=token)
    gender = models.CharField(max_length=4, choices=((0, 未知), (1, ), (2, )), default=0,
                              verbose_name=性别)
    city = models.CharField(max_length=20, null=True, blank=True, verbose_name=用户所在市)
    longitude = models.CharField(max_length=10, null=True, blank=True, verbose_name=经度)
    latitude = models.CharField(max_length=10, null=True, blank=True, verbose_name=纬度)
    jifen=models.IntegerField(default=0,verbose_name=积分)
    yongjin=models.FloatField(default=0,verbose_name=佣金)
    fensi_num=models.IntegerField(default=0,verbose_name=粉丝数)

    class Meta:
        verbose_name=用户
        verbose_name_plural = verbose_name

3.执行数据更新命令:

python manage.py makemigrations
python manage.py migrate

2.建立地区表:

1.在apps/users/models.py中建立地区表:

class Area(models.Model):
        """
        地区表
        """
        CATEGORY_TYPE = (
            (1, 省级),
            (2, 市级),
            (3, 区县级),
        )
        num=models.IntegerField(default=0,verbose_name=地区编码)
        name = models.CharField(default=‘‘, max_length=30, verbose_name=地名)
        category_type = models.IntegerField(choices=CATEGORY_TYPE, verbose_name=地区级别)
        parent_category = models.ForeignKey(self, null=True, blank=True, verbose_name=父类目录,
                                            related_name=sub_cat, on_delete=models.CASCADE)
        add_time = models.DateTimeField(default=datetime.now, verbose_name=添加时间)

        class Meta:
            verbose_name = 地区
            verbose_name_plural = verbose_name

        def __str__(self):
            return self.name

2.执行数据更新命令:

python manage.py makemigrations
python manage.py migrate

 2.ad的数据表设计:

1.安装处理图片的依赖包pillow:

pip install pillow

2.在apps/ad/models.py中:

from django.db import models
from datetime import datetime
from users.models import UserProfile,Area
from django.utils.safestring import mark_safe
# Create your models here.


class Category(models.Model):
    """
    分类表
    """
    name=models.CharField(default=‘‘,max_length=30,verbose_name=类别名)
    category_type=models.IntegerField(choices=((1, 一级类目),(2, 二级类目)),verbose_name=类别级别)
    parent_category=models.ForeignKey(self,null=True,blank=True,verbose_name=父类目录,related_name=sub_cat,on_delete=models.CASCADE)
    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 Good(models.Model):
    """广告或店铺"""
    title=models.CharField(max_length=15,null=True,blank=True,verbose_name=标题,help_text=最多15字)
    desc = models.TextField(default=‘‘, max_length=400, verbose_name=描述,help_text=最多400字)
    address = models.CharField(max_length=100, default="", verbose_name="详细地址")
    mobile = models.CharField(max_length=11, default="", verbose_name="电话")
    pv = models.IntegerField(default=0, verbose_name=浏览量)
    category = models.ForeignKey(Category, verbose_name="分类", on_delete=models.CASCADE)
    area = models.ForeignKey(Area, null=True, blank=True, verbose_name="地区", on_delete=models.CASCADE)
    stage = models.IntegerField(default=1, choices=((1, 待审核),(2, 已发布)), verbose_name="阶段")
    user = models.ForeignKey(UserProfile, verbose_name="发布者", on_delete=models.CASCADE)
    type = models.IntegerField(default=1, choices=((1, 广告), (2, 店铺)), verbose_name="类型")
    image = models.ImageField(max_length=200,default=‘‘, upload_to=smallapp/,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.title

    def image_url(self):
        return mark_safe(<img src="/media/{0}" class="field_img">.format(self.image))

    image_url.short_description = 小程序码


class Prize(models.Model):
    """奖品"""
    title = models.CharField(max_length=15, null=True, blank=True, verbose_name=标题, help_text=最多15字)
    get_num=models.IntegerField(default=0,verbose_name=被领取数)
    price = models.IntegerField(default=0, verbose_name=所需积分)
    all_num=models.IntegerField(default=0,verbose_name=发布数)
    desc = models.TextField(default=‘‘, max_length=400, verbose_name=描述, help_text=最多400字)
    usage = models.TextField(default=‘‘, max_length=200, verbose_name=使用方式, help_text=最多200字)
    image = models.ImageField(max_length=200, default=‘‘, upload_to=prizeimg/, verbose_name=奖品图片)
    end_time = models.DateTimeField(default=datetime.now, 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.title

    def image_url(self):
        return mark_safe(<img src="/media/{0}" class="field_img">.format(self.image))

    image_url.short_description = 奖品图片


class Images(models.Model):
    """
    图片
    """
    is_banner=models.BooleanField(default=False,verbose_name=是否是轮播图)
    image = models.ImageField(max_length=200, upload_to=images/)
    good = models.ForeignKey(Good, verbose_name="广告或店铺",blank=True,null=True, on_delete=models.CASCADE)
    add_time = models.DateTimeField(default=datetime.now, verbose_name=添加时间)

    class Meta:
        verbose_name = "图片"
        verbose_name_plural = verbose_name

    def image_url(self):
        return mark_safe(<img src="/media/{0}" class="field_img">.format(self.image))

    image_url.short_description = 图片

3.执行数据更新命令:

python manage.py makemigrations
python manage.py migrate

3.trade数据表设计:

1.apps/trade/models.py中:

from django.db import models
from datetime import datetime
from users.models import UserProfile,Area

# Create your models here.


class Price(models.Model):
    """置顶服务价格表"""
    top_type = models.CharField(max_length=4, choices=((0, 置顶), (1, 超级置顶)), default=0,verbose_name=置顶类型)
    price=models.FloatField(default=0,verbose_name="价格")
    fanwei=models.CharField(max_length=4, choices=((0, 全县), (1, 全市),(2, 全省)), default=0,verbose_name=置顶类型)
    days=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-%s(%d)".format(self.top_type,self.fanwei, self.days)


class Order(models.Model):
    """置顶订单"""
    user = models.ForeignKey(UserProfile, verbose_name="购买者", on_delete=models.CASCADE)
    order_sn = models.CharField(max_length=30, null=True, blank=True, unique=True, verbose_name="订单号")
    pay_status = models.CharField(choices=(("TRADE_SUCCESS", "成功"),("TRADE_CLOSED", "超时关闭"),("paying", "待支付")), default="paying", max_length=30, verbose_name="订单状态")
    trade_no = models.CharField(max_length=100, unique=True, null=True, blank=True, verbose_name="交易号")
    order_mount = models.FloatField(default=0.0, verbose_name="订单金额")
    pay_time = models.DateTimeField(null=True, blank=True, 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 ShoppingCar(models.Model):
    """购物车表(置顶)"""
    user = models.ForeignKey(UserProfile, verbose_name="购买者", on_delete=models.CASCADE)
    order = models.ForeignKey(Order, verbose_name="订单", on_delete=models.CASCADE)
    top_good=models.ForeignKey(Price, verbose_name="置顶服务种类",related_name="goods", on_delete=models.CASCADE)
    goods_num = models.IntegerField(default=1, verbose_name="置顶服务数量",help_text=目前只支持每次每种置顶服务购买数量为1)
    price=models.FloatField(default=0.0, verbose_name="订单金额")
    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")

    class Meta:
        verbose_name = "购物车表"
        verbose_name_plural = verbose_name

2.执行数据更新命令:

python manage.py makemigrations
python manage.py migrate

4.user_operation数据表设计:

1.apps/user_operation/models.py中:

from django.db import models
from datetime import datetime
from users.models import UserProfile
from ad.models import Prize
# Create your models here.


class Message(models.Model):
    """用户消息表"""
    user = models.ForeignKey(UserProfile, verbose_name="用户", on_delete=models.CASCADE)
    content=models.TextField(default=‘‘, max_length=200, verbose_name=消息内容,help_text=最多200字)
    is_read=models.BooleanField(default=False,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.content


class GetPrize(models.Model):
    """兑奖表"""
    user = models.ForeignKey(UserProfile, verbose_name="用户", on_delete=models.CASCADE)
    prize=models.ForeignKey(Prize, verbose_name="奖品", on_delete=models.CASCADE)
    status = models.CharField(choices=(("0", "待发货"), ("1", "已发货"), ("2", "已签收")),default="0",
                              max_length=8, 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.prize


class UserAdress(models.Model):
    """用户收货地址"""
    user = models.ForeignKey(UserProfile, verbose_name="用户", on_delete=models.CASCADE)
    address=models.CharField(max_length=300,verbose_name=收货地址,default=‘‘,help_text=最多300字)
    phone=models.CharField(max_length=11,verbose_name=电话,default=‘‘)
    name=models.CharField(max_length=15,verbose_name=收件人,default=‘‘)
    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 Fans(models.Model):
    """粉丝表"""
    user=models.ForeignKey(UserProfile, verbose_name="用户", related_name=user,on_delete=models.CASCADE)
    fans=models.ForeignKey(UserProfile, verbose_name="粉丝", related_name=fans,on_delete=models.CASCADE)
    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")

    class Meta:
        verbose_name = "粉丝表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.user


class MakeMoney(models.Model):
    """佣金记录表"""
    user = models.ForeignKey(UserProfile, verbose_name="用户", related_name=u,on_delete=models.CASCADE)
    fans = models.ForeignKey(UserProfile, verbose_name="粉丝", related_name=f,help_text=从哪个粉丝的消费中分成的,
                             on_delete=models.CASCADE)
    money=models.FloatField(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.user


class GetCash(models.Model):
    """提现记录表"""
    user = models.ForeignKey(UserProfile, verbose_name="用户", on_delete=models.CASCADE)
    money = models.FloatField(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.user

2.执行数据更新命令:

python manage.py makemigrations
python manage.py migrate

3.其中“提现记录表”是暂时的,等到后期如果微信的“企业提款到零钱”功能可以开放使用,则要对这个表进行修改。

 

广告小程序后端开发(2.Models设计)

上一篇:微信朋友圈测试点


下一篇:【玩转微信公众平台之八】 演示样例代码分析