3 回答

TA貢獻(xiàn)1946條經(jīng)驗 獲得超3個贊
如果沒有錯誤消息,還不清楚您的問題是什么,但我的預(yù)感是您未能在獨立腳本中引導(dǎo) Django 環(huán)境。
Django 有一個特殊的工具來構(gòu)建你可以在 Django 環(huán)境中運(yùn)行的“命令”,而不必像在 populate_first_app.py 中那樣“設(shè)置它”。
在上面的示例中,您希望將“populate_first_app.py”移動到“first_app/management/commands/populate_first_app.py”。然后你需要將你的函數(shù)放在 BaseCommand 中:
from django.core.management.base import BaseCommand, CommandError
from polls.models import Question as Poll
# .. other imports
class Command(BaseCommand):
help = 'Populates test data in first_app'
def add_arguments(self, parser):
parser.add_argument('n', type=int)
def handle(self, *args, **options):
fakegen = Faker()
for entry in range(args.n):
#.. generate each entry
一旦你有了這個命令,你就可以從 manage.py 運(yùn)行它:
manage.py populate_first_app -n 20
好處是當(dāng)別人想用這個的時候,跑起來就可以看到了
manage.py 幫助
添加回答
舉報