Answer the question
In order to leave comments, you need to log in
Where did the "category" keyword come from?
I'm trying to move data from the table to the database using the many_load.py script. When executing "python manage.py runscript many_load" in the console, it throws an error:
"Choices are: %s" % (name, ", ".join(available)))
django.core.exceptions.FieldError: Cannot resolve keyword 'category ' into fiel
d. Choices are: id, name, site"
I understand that the program does not find the keyword "category", and that the choice is only from "id", "name", "site", but there is no "category" in the model. Why does it looking for? and how to avoid this problem?
models.py:
from django.db import models
class Category(models.Model) :
name = models.CharField(max_length=512)
def __str__(self) :
return self.name
class State(models.Model) :
name = models.CharField(max_length=128)
def __str__(self):
return self.name
class Region(models.Model):
name = models.CharField(max_length=128)
def __str__(self):
return self.name
class Iso(models.Model):
name = models.CharField(max_length=8)
def __str__(self):
return self.name
class Site(models.Model):
name = models.CharField(max_length=128)
description = models.CharField(max_length=256)
justification = models.CharField(max_length=256)
year = models.IntegerField(null=True)
longitude = models.DecimalField(max_digits=20, decimal_places=10, null=True)
latitude = models.DecimalField(max_digits=20, decimal_places=10, null=True)
area_hectares = models.DecimalField(max_digits=20, decimal_places=10,null=True)
category = models.ForeignKey(Category, on_delete=models.CASCADE, null=True)
state = models.ForeignKey(State, on_delete=models.CASCADE)
region = models.ForeignKey(Region, on_delete=models.CASCADE)
iso = models.ForeignKey(Iso, on_delete=models.CASCADE)
def __str__(self) :
return self.name
import csv #
from unesco.models import Category, State, Region, Iso, Site
def run():
fhand = open('unesco/whc-sites-2018-clean.csv')
reader = csv.reader(fhand)
next(reader) # Advance past the header
Category.objects.all().delete()
State.objects.all().delete()
Region.objects.all().delete()
Iso.objects.all().delete()
Site.objects.all().delete()
for row in reader:
print(row)
c, created = Category.objects.get_or_create(category=row[7])
s, created = State.objects.get_or_create(state=row[8])
r, created = Region.objects.get_or_create(region=row[9])
i, created = Iso.objects.get_or_create(iso=row[10])
try:
y = int(row[3])
except:
y = None
st = Site(name=row[0], description=row[1], justification=row[2],
year=y, longitude=row[4], latitude=row[5], area_hectares=row[6],
category=c, state=s, region=r, iso=i)
st.save()
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question