ccnu_data 常用脚本大全

def object_list_to_dict(obj_list, primary_key='id'):
    """
    objs--->dict
    :param obj_list:
    :param primary_key:
    :return:
    """
    result = {}
    for obj in obj_list:
        key = obj.get(primary_key)
        result[key] = obj
    return result


def test_kwargs(**kwargs):
    """
    kwargs 用法实例
    :param kwargs:
    :return:
    """
    _type = type(kwargs)
    subject_id = kwargs.get('subject_id')
    paper_id = kwargs.get('paper_id')
    print _type, subject_id, paper_id

test_kwargs(
    subject_id=9, paper_id='6f94c21c-e62a-11e7-919f-005056b253b8',
    version_id='151385070243359'
)
def test_django_models_bulk_create(**kwargs):
    """
    主要测试 Django models bulk_create 功能
    :param kwargs:
    :return:
    """
    student_list_insert = []
    student_list_insert.append(
        User(
            username=kwargs.get('username') or '',
            password=kwargs.get('password') or '123456',
            personid=kwargs.get('personid') or '',
        )
    )
    try:
        User.objects.bulk_create(student_list_insert)
        return True
    except:
        return False
from django.db import models


class BaseManager(models.Manager):
    def valid(self, **kwargs):
        return self.filter(isvalid=True).filter(**kwargs)


class BaseModel(models.Model):
    isvalid = models.BooleanField(default=True)
    create_time = models.DateTimeField(auto_now_add=True)
    update_time = models.DateTimeField(auto_now=True)

    objects = BaseManager()   # 管理器接口, 数据库查询操作通过它提供给django的模型

    class Meta:
        abstract = True

    def valid_action(self, valid=True):
        self.isvalid = valid
        self.save()


你可能感兴趣的:(Python,Scripts,Python,Web,Django)