Using Django models in external python script

前端 未结 3 919
耶瑟儿~
耶瑟儿~ 2020-12-08 05:31

I was very confused today.

I was trying to use my django app models in my python script.

here is my approach

import os, sys

sys.path.append(         


        
3条回答
  •  Happy的楠姐
    2020-12-08 06:31

    The below script should work provided that the layout of your project (and the path to your settings file) looks like this:

    /var/www/cloudloon/horizon/openstack_dashboard/settings.py

    #!/usr/bin/env python
    import os, sys
    
    sys.path.append("/var/www/cloudloon/horizon")
    os.environ["DJANGO_SETTINGS_MODULE"] = "openstack_dashboard.settings"
    from django.contrib.auth.models import User
    

    I believe the problem you're seeing is due to the arrangement of your project, or that you need to append another directory level to the sys.path call in the script.

    Edit:

    Looking at your github project, horizon and openstack_dashboard are on the same directory level. What you'd want to do is set your sys.path to one level higher:

    sys.path.append("/var/www/cloudloon")
    

提交回复
热议问题