Skip to content

Instantly share code, notes, and snippets.

@riku179
Last active September 7, 2016 13:38
Show Gist options
  • Save riku179/1a122803b09ad5ddd6fcad6a1b5b8ec3 to your computer and use it in GitHub Desktop.
Save riku179/1a122803b09ad5ddd6fcad6a1b5b8ec3 to your computer and use it in GitHub Desktop.

djangoの雑多なメモ(Django 1.10)

djangoのモデルを外部から利用

import sys, os, django
sys.path.append('/path/to/My/Project')
os.environ['DJANGO_SETTINGS_MODULE'] = 'MyProject.settings'
django.setup()

from myapp.models import Mymodel
hoge = Mymodel.objects.get(id=1)

現在時刻を取得(タイムゾーンのアレコレを解決)

import datetime
from django.utils.timezone import utc

now = datetime.datetime.utcnow().replace(tzinfo=utc)

MySQL(MariaDB)の使用

project/settings.pyDATABASE_SETTINGSのあたりはググれば出る Django公式にRecommendされているmysqlclientをインストールするにはmariadb-develが必要(CentOS7)

# yum install mariadb-devel
# pip install mysqlclient
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment