首页 > 数据库技术 > 详细

Django终端打印SQL语句和调用test脚本

时间:2018-06-23 12:41:25      阅读:231      评论:0      收藏:0      [点我收藏+]

Django终端打印SQL语句

LOGGING = {
    version: 1,
    disable_existing_loggers: False,
    handlers: {
        console:{
            level:DEBUG,
            class:logging.StreamHandler,
        },
    },
    loggers: {
        django.db.backends: {
            handlers: [console],
            propagate: True,
            level:DEBUG,
        },
    }
}

在Python脚本中调用Django环境

import os

if __name__ == __main__:
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "BMS.settings")
    import django
    django.setup()

    from app01 import models

    books = models.Book.objects.all()
    print(books)

 

LOGGING = {
    version: 1,
    disable_existing_loggers: False,
    handlers: {
        console:{
            level:DEBUG,
            class:logging.StreamHandler,
        },
    },
    loggers: {
        django.db.backends: {
            handlers: [console],
            propagate: True,
            level:DEBUG,
        },
    }
}

在Python脚本中调用Django环境

import os

if __name__ == __main__:
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "BMS.settings")
    import django
    django.setup()

    from app01 import models

    books = models.Book.objects.all()
    print(books)

 

django中自定义表名称

class Record(models.Model):
    pass
    class Meta:
        db_table="Record"

在djanggo中自定义char类型

# 自定义char类型
class FixCharField(models.Field):
    def __init__(self, max_length, *args, **kwargs):
        self.max_length = max_length
        super().__init__(max_length=self.max_length,*args, **kwargs)

    def db_type(self, connection):
        return ‘char(%s)‘ % self.max_length

 

Django终端打印SQL语句和调用test脚本

原文:https://www.cnblogs.com/dingyutao/p/9216638.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!