[英]Django management commands is not able to find my application
我在運行自定義命令時遇到問題,因為它將引發NameError:未定義全局名稱“ graphofknowledge”。 我的文件結構是
projectFile |-manage.py |-.. |-graphofknowledge (app) |-models.py |-views.py |-management |-__init__.py |-commands |-__init__.py |-customCommand.py
這是我的自定義命令的代碼
from django.core.management.base import BaseCommand
from graphofknowledge.models import TagTrend_refine
class Command(BaseCommand):
args = '<foo bar ...>'
help = 'our help string comes here'
def loadTagTrend(self, fileName):
listOfData = []
f = open(fileName)
lines = f.readlines()
f.close()
for line in lines:
temp = line.strip().split("\t")
data = TagTrend_refine(
tag = temp[0],
trendData = temp[1]
)
listOfData.append(data)
TagTrend_refine.objects.bulk_create(listOfEntities)
def handle(self, *args, **options):
self.loadTagTrend(graphofknowledge/tagTrend_refine.txt)
我將應用程序名稱添加到已安裝的應用程序中。 當我在自定義命令中運行打印輸出時,它可以工作。 但是,一旦我為模型添加了import語句,它將拋出NameError。 我可以知道如何解決這個問題嗎?
您似乎忘記了文件名字符串使用引號。 嘗試:
self.loadTagTrend('graphofknowledge/tagTrend_refine.txt')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.