How to execute external script in the Django environment

后端 未结 3 1242
孤独总比滥情好
孤独总比滥情好 2020-12-15 09:26

I am trying to execute an external snippet for debugging and terminal-like purposes in the environment the Django console uses so it can connect to the db, etc.

Basi

3条回答
  •  北海茫月
    2020-12-15 10:25

    If its just a one off script,

    import django
    django.setup()
    
    from myapp.models.import MyModel 
    

    You need to have your environmental variable set up, so its easiest to run it from the IDE (make it part of the same project, right click on teh file and there should be a run option).

    If you are looking for something to run on a production environment I would create a management command as suggested by DanEEStar.

提交回复
热议问题