python - 如何调试无法导入“adoptions.models”?
问题描述
错误表示无法导入“adoptions.models”并且在未绑定的方法调用中参数“dt”没有值。
程序:
from csv import DictReader
from datetime import datetime
from django.core.management import BaseCommand
from adoptions.models import Pet, Vaccine
from pytz import UTC
DATETIME_FORMAT = '%m/%d/%Y %H:%M'
VACCINES_NAMES = [
'Canine Parvo',
'Canine Distemper',
'Canine Rabies',
'Canine Leptospira',
'Feline Herpes Virus 1',
'Feline Rabies',
'Feline Leukemia'
]
ALREADY_LOADED_ERROR_MESSAGE = """
If you need to reload the pet data from the CSV file,
first delete the db.sqlite3 file to destroy the database.
Then, run `python manage.py migrate` for a new empty
database with tables"""
class Command(BaseCommand):
# Show this when the user types help
help = "Loads data from pet_data.csv into our Pet model"
def handle(self, *args, **options):
if Vaccine.objects.exists() or Pet.objects.exists():
print('Pet data already loaded...exiting.')
print(ALREADY_LOADED_ERROR_MESSAGE)
return
print("Creating vaccine data")
for vaccine_name in VACCINES_NAMES:
vac = Vaccine(name=vaccine_name)
vac.save()
print("Loading pet data for pets available for adoption")
for row in DictReader(open('./pet_data.csv')):
pet = Pet()
pet.name = row['Pet']
pet.submitter = row['Submitter']
pet.species = row['Species']
pet.breed = row['Breed']
pet.description = row['Pet Description']
pet.sex = row['Sex']
pet.age = row['Age']
raw_submission_date = row['submission date']
submission_date = UTC.localize(
datetime.strptime(raw_submission_date, DATETIME_FORMAT))
pet.submission_date = submission_date
pet.save()
raw_vaccination_names = row['vaccinations']
vaccination_names = [name for name in raw_vaccination_names.split('| ') if name]
for vac_name in vaccination_names:
vac = Vaccine.objects.get(name=vac_name)
pet.vaccinations.add(vac)
pet.save()
解决方案
推荐阅读
- angular - Angular:使用 ChangeDetectionStrategy.OnPush 进行模型侦听
- android - SyncAdapter 在 Kotlin 中更改数据后更新 RecyclerView UI
- reactjs - React:如果 useCallback 返回一个值可以吗,或者这是一个不好的模式?
- c++ - 在 C++ 中为链表类实现入队函数
- java - 我将双倍时间“”的方法错误 - 带有特定文本的 XPATH
- html - 在 html5 中播放来自谷歌驱动器的 .avi 文件
- python-3.x - 在所有任务启动之前恢复异步任务
- javascript - 为什么我们要在代码中为参数赋值,而它会根据用户输入而改变?
- regular-language - PDA 和正则表达式
- reactjs - TypeError:无法读取 React 中未定义的属性“imageUrl”