나는 보통 connection
개체 자체에서이 작업을 수행합니다.
from django.db import connections
cursor = connections['DATABASE_NAME'].cursor()
# replace DATABASE_NAME with the name of your
# DATABASE connection, i.e., the appropriate key in the
# settings.DATABASES dictionary
cursor.execute("set names utf8mb4;")
이렇게하면 임의의 모델을 사용하여 원시 쿼리를 실행하지 않아도됩니다.
n.b. 당신은 단지 즉, 하나 개의 데이터베이스 연결이있는 경우, default
당신이 사용할 수 있습니다
from django.db import connection
cursor = connection.cursor()
cursor.execute("set names utf8mb4;")
시작할 때 한번를 실행하려면 추가 할 수 있습니다이 유용 할 수 있습니다 귀하의 DATABASES
DATABASES = {
'default': {
...
'OPTIONS': {
"init_command": "set names utf8mb4;"
}
}
}
출처
2016-12-17 04:11:24
2ps
에 다음과 같은 : http://stackoverflow.com/questions/6791911/execute-code-when-django-starts-once-only –