Django: How can I use my model classes to interact with my database from outside Django?

后端 未结 3 716
囚心锁ツ
囚心锁ツ 2020-12-23 18:29

I\'d like to write a script that interacts with my DB using a Django app\'s model. However, I would like to be able to run this script from the command line or via cron. W

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-23 18:47

    Depending on your specific needs, django-command-extensions might save you a bit of time. To run any script as-is without messing around with environment variables just type:

    ./manage.py runscript path/to/my/script.py
    

    django-command-extensions also has commands for automating scripts as cron jobs, which is something you mentioned that you'd like to do.

    If you are a more nuts and bolts type of person, you might check out this very detailed post outlining how to make "standalone" django scripts to be run from cron jobs and whatnot.

提交回复
热议问题