Accessing django models from a new script

后端 未结 3 2079
逝去的感伤
逝去的感伤 2021-02-11 09:42

I have seen many answers on how to use django models outside the project. But I have no success with any. I was trying to implement this answer but I get an error. I have create

3条回答
  •  南笙
    南笙 (楼主)
    2021-02-11 10:26

    You need is importable settings.

    import os
    import django
    os.environ["DJANGO_SETTINGS_MODULE"] = 'project.settings'
    django.setup()
    from .models import 
    

    Another way call your script via the django shell:

    python manage.py shell < script.py
    

提交回复
热议问题