[Django] Manage django database using additional scripts

  • If you just want to use django ORM, please refer to Django ORM Standalone Template.
  • If you want to use other python code to manipulate existing django database inside a project, using its pretty ORM, please do as following code:
# setup django environment using existing settings file
import os
import django

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "yourproject.settings")
django.setup()

# start of main code
from myapp.models import *

testobjs = mymodel.objects.all()
print(testobjs)

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *