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

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)

 

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

 

posted @ 2018-06-23 11:02  似水年华梦  阅读(347)  评论(0编辑  收藏  举报