“ django.db.utils.IntegrityError:” NOT NULL约束失败:options_pet.submission_data

时间:2018-11-13 07:39:37

标签: python django

我正在尝试用CSV文件中的测试数据填充我的收养网站

  

django.db.utils.IntegrityError:NOT NULL约束失败:   options_pet.submission_data

我跑步
python manage.py makemigrations
然后我跑
python manage.py migration
之后,我编写了一个命令来运行名为 load_pet_data.py 的脚本文件,该命令会将测试数据插入该命令所在的文件中
python manage.py load_pet_data

经过一些搜索,我试图删除“ db.sqlit3” 并重新迁移,但是我仍然遇到相同的错误
 -这是load_pet_data.py脚本文件

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()

这是我的模型编写所在的应用程序“ models.py”

from django.db import models

class Pet(models.Model):
    SEX_CHOICES=[('M','Male'),('F','Female')]
    name=models.CharField(max_length=100)
    submitter=models.CharField(max_length=100)
    species=models.CharField(max_length=30)
    breed=models.CharField(max_length=30,blank=True)
    description = models.TextField()
    sex=models.CharField(choices=SEX_CHOICES,max_length=1,blank=True)
    submission_data=models.DateTimeField()
    age=models.IntegerField(null=True)
    vaccinations=models.ManyToManyField('Vaccine',blank=True)
class Vaccine (models.Model):
    name=models.CharField(max_length=50)

这是项目建设的屏幕快照,可能有帮助 This is a screen shoot for the project construction may it help

2 个答案:

答案 0 :(得分:1)

您有错字。应该是pet.submission_data而不是pet.submission_date = submission_date

答案 1 :(得分:1)

您将字段名指定为submission_date

            submission_date = UTC.localize(
                datetime.strptime(raw_submission_date, DATETIME_FORMAT))
            pet.submission_date = submission_date

但是您的模型有

submission_data=models.DateTimeField()