Django Standalone Script

后端 未结 4 1475
死守一世寂寞
死守一世寂寞 2020-12-01 07:43

I am trying to access my Django (v1.10) app DB from another python script and having some trouble doing so.

This is my file and folder structure:

sto         


        
4条回答
  •  青春惊慌失措
    2020-12-01 08:13

    Try this

    import sys, os, django
    sys.path.append("/path/to/store") #here store is root folder(means parent).
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "store.settings")
    django.setup()
    
    from store_app.models import MyModel
    

    This script you can use anywhere in your system.

提交回复
热议问题