表结构设计
abstract类
django中orm自定义的
from django.db import models
class ActiveBaseModel(models.Model):
active = models.SmallIntegerField(verbose_name="状态", default=1, choices=((1, "激活"), (0, "删除"),))
class Meta:
abstract = True
class Administrator(ActiveBaseModel):
""" 管理员表 """
username = models.CharField(verbose_name="用户名", max_length=32, db_index=True)
password = models.CharField(verbose_name="密码", max_length=64)
mobile = models.CharField(verbose_name="手机号", max_length=11, db_index=True)
create_date = models.DateTimeField(verbose_name="创建日期", auto_now_add=True)
物理删除&&逻辑删除
class Administrator(model.Model):
""" 管理员表 """
username = models.CharField(verbose_name="用户名", max_length=32, db_index=True)
password = models.CharField(verbose_name="密码", max_length=64)
active = models.SmallIntegerField(verbose_name="状态", default=1, choices=((1, "激活"), (0, "删除"),))
# 物理删除,你就真把东西删除完了
queryset = models.Administrator.objects.all()
models.Administrator.objects.filter(id=10).delete()
# 逻辑删除,实际上数据还在,依旧可以恢复
queryset = models.Administrator.objects.filter(active=1)
models.Administrator.objects.filter(id=10).update(active=0)
自增和主键
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
class Administrator(model.Model):
""" 管理员表 """
#比如自动创建id
id = models.BigAutoField(primary_key=True) #其实是数据库的类型 long
bid = models.AutoField(primary_key=True) #int
xid = models.SmallAutoField(primary_key=True) #tiny int
username = models.CharField(verbose_name="用户名", max_length=32, db_index=True)
password = models.CharField(verbose_name="密码", max_length=64)
具体设计
from django.db import models
class ActiveBaseModel(models.Model):
active = models.SmallIntegerField(verbose_name="状态", default=1, choices=((1, "激活"), (0, "删除"),))
class Meta:
abstract = True
class Administrator(ActiveBaseModel):
""" 管理员表 """
#db_index是在用索引查询,所谓索引查询等价于创建了一个文件B+树的算法来查询
username = models.CharField(verbose_name="用户名", max_length=32, db_index=True)
password = models.CharField(verbose_name="密码", max_length=64)
mobile = models.CharField(verbose_name="手机号", max_length=11, db_index=True)
create_date = models.DateTimeField(verbose_name="创建日期", auto_now_add=True)
class Level(ActiveBaseModel):
""" 级别表 """
title = models.CharField(verbose_name="标题", max_length=32)
percent = models.IntegerField(verbose_name="折扣", help_text="填入0-100整数表示百分比,例如:90,表示90%")
def __str__(self):
return self.title
class Customer(ActiveBaseModel):
""" 客户表 """
username = models.CharField(verbose_name="用户名", max_length=32, db_index=True)
password = models.CharField(verbose_name="密码", max_length=64)
mobile = models.CharField(verbose_name="手机号", max_length=11, db_index=True)
# mobile = models.CharField(verbose_name="手机号", max_length=11, db_index=True, validators=[RegexValidator(r'^\d{11}$', '手机号格式错误'), ], )
#DecimalField表示十进制数,方便存储,更为精准
balance = models.DecimalField(verbose_name="账户余额", default=0, max_digits=10, decimal_places=2)
level = models.ForeignKey(verbose_name="级别", to="Level", on_delete=models.CASCADE)
# level = models.ForeignKey(verbose_name="级别", to="Level", on_delete=models.CASCADE, limit_choices_to={'active': 1})
create_date = models.DateTimeField(verbose_name="创建日期", auto_now_add=True)
#on_delete=models.CASCADE。是为了解决特殊情况下的问题,假如有一个部门表,员工表关联了部门表。假如删除部门表的一个部门
#对应的员工表全部干掉。
#models.set_NULL。也可以置为空(blank=True, null=true)
#model.set_defalut 设为默认值。
#model.protected 受保护,提示报错。可以手动转移数据。
creator = models.ForeignKey(verbose_name="创建者", to="Administrator", on_delete=models.CASCADE)
class PricePolicy(models.Model):
""" 价格策略(原价,后续可以根据用级别不同做不同折扣)
1 1000 10
2 2000 18
"""
count = models.IntegerField(verbose_name="数量")
price = models.DecimalField(verbose_name="价格", default=0, max_digits=10, decimal_places=2)
class Order(ActiveBaseModel):
""" 订单表 """
status_choices = (
(1, "待执行"),
(2, "正在执行"),
(3, "已完成"),
(4, "失败"),
(5, "已撤单"),
)
status = models.SmallIntegerField(verbose_name="状态", choices=status_choices, default=1)
# 202211022123123123
oid = models.CharField(verbose_name="订单号", max_length=64, unique=True)
url = models.URLField(verbose_name="视频地址", db_index=True)
count = models.IntegerField(verbose_name="数量")
price = models.DecimalField(verbose_name="价格", default=0, max_digits=10, decimal_places=2)
real_price = models.DecimalField(verbose_name="实际价格", default=0, max_digits=10, decimal_places=2)
old_view_count = models.CharField(verbose_name="原播放量", max_length=32, default="0")
create_datetime = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)
customer = models.ForeignKey(verbose_name="客户", to="Customer", on_delete=models.CASCADE)
memo = models.TextField(verbose_name="备注", null=True, blank=True)
class TransactionRecord(ActiveBaseModel):
""" 交易记录 """
charge_type_class_mapping = {
1: "success",
2: "danger",
3: "default",
4: "info",
5: "primary",
}
charge_type_choices = ((1, "充值"), (2, "扣款"), (3, "创建订单"), (4, "删除订单"), (5, "撤单"),)
charge_type = models.SmallIntegerField(verbose_name="类型", choices=charge_type_choices)
customer = models.ForeignKey(verbose_name="客户", to="Customer", on_delete=models.CASCADE)
amount = models.DecimalField(verbose_name="金额", default=0, max_digits=10, decimal_places=2)
creator = models.ForeignKey(verbose_name="管理员", to="Administrator", on_delete=models.CASCADE, null=True, blank=True)
order_oid = models.CharField(verbose_name="订单号", max_length=64, null=True, blank=True, db_index=True)
create_datetime = models.DateTimeField(verbose_name="交易时间", auto_now_add=True)
memo = models.TextField(verbose_name="备注", null=True, blank=True)
开始项目
#settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'my_djpro', # 数据库名字
'USER': 'root',
'PASSWORD': 'root123',
'HOST': '127.0.0.1', # 那台机器安装了MySQL
'PORT': 3306,
}
}
python manage.py makemigrations
python manage.py migrate
缓存和Session
windows这里可以自行必应搜索安装
pip install django-redis
#settings.py
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"CONNECTION_POOL_KWARGS": {"max_connections": 100},
"PASSWORD": "qwe123",
# 'MAX_ENTRIES': 300, # 最大缓存个数(默认300)
# 'CULL_FREQUENCY': 3, # 缓存到达最大个数之后,剔除缓存个数的比例,即:1/CULL_FREQUENCY(默认3)
}
}
}
认证,动态菜单,权限控制
简单来说,各个身份的人看到的菜单应该不同。