1. 程式人生 > >Django終端打印SQL語句和調用test腳本

Django終端打印SQL語句和調用test腳本

code end logs urn cti 打印 pri field Go

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腳本