Use the Django ORM in a standalone script (again)

前端 未结 3 1462
情歌与酒
情歌与酒 2021-02-01 11:07

I\'m trying to use the Django ORM in some standalone screen scraping scripts. I know this question has been asked before, but I\'m unable to figure out a good solution for my pa

3条回答
  •  不思量自难忘°
    2021-02-01 11:31

    Found an easy way to reuse existing django app's settings for console script:

    from django.core.management import setup_environ
    
    import settings
    setup_environ(settings)
    
    from myapp.models import Object
    
    for o in Object.objects.all():
        print o
    

提交回复
热议问题