• models管理类抽取基类


    Models类 models.py

    
    # coding:utf-8
    
    from django.db import models
    from db.Base_model import Base_Model
    from db.Base_manager import BaseManager
    
    class UserInfoManager(BaseManager):
            def creat(self, username, password, email):
            '''添加对象'''
            userinfo = self.creat_one_object(username=username, password=password, email=email)
            return userinfo
    
        def select(self, username, password=None):
            '''查询对象'''
            print '%s--zh_cn-' % password
            if password is None:
                userinfo = self.get_one_object(username=username)
            else:
                userinfo = self.get_one_object(username=username, password=password)
            return userinfo
    
    
    class AddressManager(BaseManager):
        '''地址模型类管理器'''
    
        def get_default_address(self, userinfo_id):
            '''获取用户默认收货信息'''
            addr = self.get_one_object(userinfo_id=userinfo_id, is_def=True)
            return addr
    
        def add_one_address(self, userinfo_id, recipient_name, recipient_addr, recipient_phone, zip_code):
            '''添加一个用户收货信息'''
            # 查询用户是否有默认地址
            def_addr = self.get_one_object(userinfo_id=userinfo_id)
            if def_addr is None:
                # 如果用户么有默认地址
                addr = self.add_one_address(userinfo_id=userinfo_id, recipient_name=recipient_name,
                                            recipient_addr=recipient_addr,
                                            recipient_phone=recipient_phone, zip_code=zip_code, is_def=True)
            else:
                addr = self.add_one_address(userinfo_id=userinfo_id, recipient_name=recipient_name,
                                            recipient_addr=recipient_addr,
                                            recipient_phone=recipient_phone, zip_code=zip_code)
            return addr
    
    
    class UserInfo(Base_Model):
        username = models.CharField(verbose_name='用户名', max_length=20)
        password = models.CharField(verbose_name='密码', max_length=40)
        email = models.EmailField(verbose_name='邮箱')
    
        objects = UserInfoManager()  # 创建管理类对象
    
        class Meta:
            db_table = 'userinfos'  # 指定表名
    
    
    class Adderss(Base_Model):
        userinfo = models.ForeignKey('UserInfo', verbose_name='所属账户')
        recipient_name = models.CharField(max_length=20, verbose_name='收货人')
        recipient_phone = models.CharField(max_length=11, verbose_name='收货电话')
        recipient_addr = models.CharField(max_length=256, verbose_name='收货地址')
        zip_code = models.CharField(max_length=6, verbose_name='邮编')
        is_def = models.BooleanField(default=False, verbose_name='默认地址')
    
        objects = AddressManager()
    
        class Meta:
            db_table = 's_user_address'
    
    
    
    
    
    
    

    BaseManager.py

    # coding=utf-8
    from django.db import models
    import copy
    
    
    
    class BaseManager(models.Manager):
        '''定义管理器基类'''
        def get_all_valid_fields(self):
            '''获取self所在模型类的有效属性字符串列表'''
            # 1. 获取self所在的模型类
            models_class = self.model
            # 2. 获取模型类的属性元组
            attr_tuple = models_class._meta.get_fields()
            # 3. 定义一个列表用来保存满足的属性名称
            str_attr_list = []
            for attr in attr_tuple:
                # 如果有属性 是外键
                if isinstance(attr,models.ForeignKey):
                    str_attr = '%s_id'%attr.name
                else:
                    str_attr = attr.name
                str_attr_list.append(str_attr)
            return str_attr_list
    
    # 增
        def creat_one_object(self,**kwargs):
            '''创建一个self所在模型类的对象'''
            # 1.获取self所在模型类的有效属性字符串列表
            valid_fields = self.get_all_valid_fields()
            # 2.拷贝一份用来处理
            kws = copy.copy(**kwargs)
            # 3.去除kws 参数中self.model的无效属性
            for key in kws:
                if key not in valid_fields:
                    kwargs.pop(key)
            # 4.获取self所在模型类
            model_class = self.model
            # 5.创建一个模型类对象
            obj = model_class(**kwargs)
            # 6.调用对象的save方法保存进数据库
            obj.save()
            # 7.返回这个对象
            return obj
    
    # 查
        def get_one_object(self,**filters):
            '''根据filters条件查询self.model模型类的对象'''
            try:
                obj = self.get(**filters)
            except self.model.DoesNotExist:
                obj = None
            return obj
    
    
    # 根据条件查询self模型的查询集
        def get_object_list(self, filters={}, exclud_filters={}, order_by=('-pk',)):
            object_list = self.filter(**filters).exclude(**exclud_filters).order_by(*order_by)
    
            return object_list
    
    
    
    
    
    # 改
    
    
    #删
    
    
    
    
  • 相关阅读:
    [WPF VTK]三维图形开发基础(一)
    WP开发(一)
    [WPF VTK]三维图形开发基础(四)

    WIN8 下IE突然无法打开(管理员权限可打开)
    [WPF VTK]三维图形开发基础(三)
    堆排序、快排的坑
    双向链表之插入
    [WPF VTK]三维图形开发基础(二)
    [转载]Android界面设计学习日志(一)
  • 原文地址:https://www.cnblogs.com/zhuchen/p/7442447.html
Copyright © 2020-2023  润新知