Django的models(二)

管理器Manager
管理器是Django的模型进行数据库的查询操作的接口

from django.db import models

# 添加一个UserInfo管理类
# 这是继承models.Manager类
class UserInfoManage(models.Manager):
    """管理类"""
    def get_queryset(self):
        return super(UserInfoManage, self).get_queryset().filter(is_delete=False)

    def create(self, user_name, password, email):
        m = UserInfo()
        m.user_name = user_name
        m.password = password
        m.email = email
        m.save()
        return m

    def update(self):
        pass

class UserInfo(models.Model):
    """这是一个用户信息的类"""
    user_name = models.CharField(max_length=20)
    password = models.CharField(max_length=40)
    email = models.CharField(max_length=20)
    address = models.CharField(max_length=30, default='')
    recipient = models.CharField(max_length=20, default='')
    post_code = models.CharField(max_length=6, default='')
    phone = models.CharField(max_length=11, default='')
    is_delete = models.BooleanField(default=False)

    # 添加一个属性来调用UserInfoManage()
    manage = UserInfoManage()

在视图中调用时UserInfo.manage.create(user_name, password, email)就可以创建一个新的数据

你可能感兴趣的:(Django的models(二))