Doing the "Become a Django dev" on LI Learning. However, I have trouble getting the column date values of a .csv into my website.
The error goes, after visiting http://localhost:8000/admin/adoptions/pet/:
File "C:UsersDELLAppDataLocalProgramsPythonPython39libsite-packagesdjangodbackendssqlite3ase.py", line 396, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such column: adoptions_pet.submissions_date
The db.sqlite3 file looks fine in my DB browser.
db browser image
models.py file:
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(max_length=1, choices=SEX_CHOICES, blank=True)
submissions_date = models.DateTimeField()
age = models.IntegerField(null=True)
vaccinations = models.ManyToManyField('Vaccine', blank=True)
class Vaccine(models.Model):
name = models.CharField(max_length=50)
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'
]
ALREDY_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 mode"
def handle(self, *args, **options):
if Vaccine.objects.exists() or Pet.objects.exists():
print('Pet data already loaded...exiting.')
print(ALREDY_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()
I have the feeling something is going wrong here, but I'm not sure:
raw_submission_date = row['submission date']
submission_date = UTC.localize(
datetime.strptime(raw_submission_date, DATETIME_FORMAT))
pet.submission_date = submission_date
The traceback I get on http://localhost:8000/admin/adoptions/pet/
Environment:
Request Method: GET
Request URL: http://localhost:8000/admin/adoptions/pet/
Django Version: 3.0.3
Python Version: 3.9.1
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'adoptions']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback (most recent call last):
File "C:UsersDELLAppDataLocalProgramsPythonPython39libsite-packagesdjangodbackendsutils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "C:UsersDELLAppDataLocalProgramsPythonPython39libsite-packagesdjangodbackendssqlite3ase.py", line 396, in execute
return Database.Cursor.execute(self, query, params)
The above exception (no such column: adoptions_pet.submissions_date) was the direct cause of the following exception:
File "C:UsersDELLAppDataLocalProgramsPythonPython39libsite-packagesdjangocorehandlersexception.py", line 34, in inner
response = get_response(request)
File "C:UsersDELLAppDataLocalProgramsPythonPython39libsite-packagesdjangocorehandlersase.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:UsersDELLAppDataLocalProgramsPythonPython39libsite-packagesdjangocorehandlersase.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:UsersDELLAppDataLocalProgramsPythonPython39libsite-packagesdjangocontribadminoptions.py", line 607, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "C:UsersDELLAppDataLocalProgramsPythonPython39libsite-packagesdjangoutilsdecorators.py", line 130, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "C:UsersDELLAppDataLocalProgramsPythonPython39libsite-packagesdjangoviewsdecoratorscache.py", line 44, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "C:UsersDELLAppDataLocalProgramsPythonPython39libsite-packagesdjangocontribadminsites.py", line 231, in inner
return view(request, *args, **kwargs)
File "C:UsersDELLAppDataLocalProgramsPythonPython39libsite-packagesdjangoutilsdecorators.py", line 43, in _wrapper
return bound_method(*args, **kwargs)
File "C:UsersDELLAppDataLocalProgramsPythonPython39libsite-packagesdjangoutilsdecorators.py", line 130, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "C:UsersDELLAppDataLocalProgramsPythonPython39libsite-packagesdjangocontribadminoptions.py", line 1796, in changelist_view
'selection_note': _('0 of %(cnt)s selected') % {'cnt': len(cl.result_list)},
File "C:UsersDELLAppDataLocalProgramsPythonPython39libsite-packagesdjangodbmodelsquery.py", line 258, in __len__
self._fetch_all()
File "C:UsersDELLAppDataLocalProgramsPythonPython39libsite-packagesdjangodbmodelsquery.py", line 1261, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "C:UsersDELLAppDataLocalProgramsPythonPython39libsite-packagesdjangodbmodelsquery.py", line 57, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "C:UsersDELLAppDataLocalProgramsPythonPython39libsite-packagesdjangodbmodelssqlcompiler.py", line 1144, in execute_sql
cursor.execute(sql, params)
File "C:UsersDELLAppDataLocalProgramsPythonPython39libsite-packagesdjangodbackendsutils.py", line 100, in execute
return super().execute(sql, params)
File "C:UsersDELLAppDataLocalProgramsPythonPython39libsite-packagesdjangodbackendsutils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:UsersDELLAppDataLocalProgramsPythonPython39libsite-packagesdjangodbackendsutils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:UsersDELLAppDataLocalProgramsPythonPython39libsite-packagesdjangodbackendsutils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "C:UsersDELLAppDataLocalProgramsPythonPython39libsite-packagesdjangodbutils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:UsersDELLAppDataLocalProgramsPythonPython39libsite-packagesdjangodbackendsutils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "C:UsersDELLAppDataLocalProgramsPythonPython39libsite-packagesdjangodbackendssqlite3ase.py", line 396, in execute
return Database.Cursor.execute(self, query, params)
Exception Type: OperationalError at /admin/adoptions/pet/
Exception Value: no such column: adoptions_pet.submissions_date