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()
VACCINES_NAMES = [ 'Canine Parvovirus', 'Canine Distemper', 'Canine Rabies', 'Feline Herpes Virus 1', 'Feline Rabies' ] for vaccine_name in VACCINES_NAMES: vacs = Vaccine.objects.filter(name=vaccine_name) if (len(vacs) == 0): vac = Vaccine(name=vaccine_name) vac.save() for row in data: if row[0] != 'create_date': pet = Pet() pet.name = row[0] pet.species = row[1] pet.breed = row[2] pet.sex = row[3] pet.age = int(row[4]) pet.submitter = row[5] pet.submission_date = datetime.strptime(row[6], '%m/%d/%Y') pet.description = row[7] pet.picture = row[9] pet.save() # Many-to-Many relation raw_vaccinations_names = row[8] vaccinations = raw_vaccinations_names.split(' | ') for vaccine in vaccinations: vaccine_obj = Vaccine.objects.get(name=vaccine)