我正在处理我的第一个Django应用程序,但我在运行自定义python脚本时遇到问题。我创建了一个名为awardnominee的应用程序,并更新了model.py以创建AwardNominee
。我还在里面创建了一个目录management/commands/
,我添加了一个名为addawarddata.py
的脚本。每当我运行命令python3 manage.py addawarddata
时,我都会收到错误
ModuleNotFoundError: No module named 'awardnominee.management.conferencesite'
问题在于我的import语句
from conferencesite.mysite.awardnominee.models import AwardNominee
。如果我路径使用这两个选项引用我的模型本地我收到相应的错误。
from ....awardnominee.models import AwardNominee
ValueError: attempted relative import beyond top-level package
或
from ....awardnominee.models import AwardNominee
ModuleNotFoundError: No module named 'awardnominee.management.awardnominee'
最后
from awardnominee.models import AwardNominee
attributeError: module 'awardnominee.management.commands.addawarddata' has no attribute 'Command'
我在下面发布了我的代码。任何帮助,将不胜感激。
models.py
from django.db import models
# Create your models here.
class AwardNominee(models.Model):
nominee_name = models.CharField(max_length=200)
nominee_desc = models.CharField(max_length=200)
# my nominee photos streamed from urls
nominee_img_src = models.CharField(max_length=200)
nominee_votes = models.IntegerField(default=0)
aaddawarddata.py
# from conferencesite.mysite.awardnominee.models import AwardNominee
from awardnominee.models import AwardNominee
nominee1 = AwardNominee(
nominee_name="Team 1's app",
nominee_desc="Team 1 created an Android mobile application for pets.",
nominee_img_src="https://images.unsplash.com/photo-1490222939321-2a267366a124?auto=format&fit="
"crop&w=1050&q=60&ixid=dW5zcGxhc2guY29tOzs7Ozs%3D",
nominee_votes=0)
nominee1.save()
nominee2 = AwardNominee(
nominee_name="Team 2's app",
nominee_desc="Team 2 created Android mobile application for finding diners.",
nominee_img_src="https://images.unsplash.com/photo-1490222939321-2a267366a124?auto=format&fit=crop&w=1050&q="
"60&ixid=dW5zcGxhc2guY29tOzs7Ozs%3D",
nominee_votes=0)
nominee2.save()
nominee3 = AwardNominee(
nominee_name="Team 3's app",
nominee_desc="Team 3 created an Ionic mobile application for pets.",
nominee_img_src="https://images.unsplash.com/photo-1494366222322-387658a1a976?auto=format&fit=crop&w=1051&q="
"60&ixid=dW5zcGxhc2guY29tOzs7Ozs%3D",
nominee_votes=0)
nominee3.save()