model in my application:
from __future__ import unicode_literals
from django.db import models
class Category(models.Model):
name = models.CharField(max_length = 128, unique = True)
def __unicode__(self):
return self.name
class ModelName(models.Model):
name = models.CharField(max_length = 128, unique = True)
def __unicode__(self):
return self.name
class CarStorage(models.Model):
category = models.ForeignKey(Category)
CarID = models.CharField(max_length = 10, unique = True, null = False)
model = models.ForeignKey(ModelName)
storage = models.IntegerField(default = 0)
perchasedYear = models.IntegerField(default = 0)
def __unicode__(self):
return self.CarID
Population code:
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE','TKTrading.settings')
import django
django.setup()
from APP.models import Category, ModelName, CarStorage
def populate():
BASE_DIR = '/Users/apple/WebWorkshop/TKTrading/'
DATA_DIR = os.path.join(BASE_DIR, 'APP/Data')
CATEGORY = open(os.path.join(DATA_DIR, 'CATEGORY.txt'),'r')
global CategoryList
CategoryList = []
for line in CATEGORY:
newC = Add_Category(line)
CategoryList.append(newC)
CATEGORY.close
MODELNAME = open(os.path.join(DATA_DIR, 'Model.txt'), 'r')
global ModelList
ModelList = []
for line2 in MODELNAME:
newM = Add_ModelName(line2)
ModelList.append(newM)
MODELNAME.close
CARSTORAGE = open(os.path.join(DATA_DIR, 'CARSTORAGE.txt'), 'r')
for line3 in CARSTORAGE:
print(line3)
c = line3.split(',')
ID = str(c[0])
category = str(c[1])
model = str(c[2])
storage = int(c[3])
year = int(c[4])
Add_CarStorage(ID, category, model, storage, year)
CARSTORAGE.close
def Add_Category(name):
c = Category.objects.get_or_create(name = name)[0]
c.save()
return c
def Add_ModelName(name):
m = ModelName.objects.get_or_create(name = name)[0]
m.save()
return m
def Add_CarStorage(ID, category, model, storage, year):
for CATEGORY in CategoryList:
if CATEGORY.name == category:
category = CATEGORY
for MODEL in ModelList:
if MODEL.name == model:
model = MODEL
car = CarStorage.objects.get_or_create(CarID = ID,
category = category,
model = model,)[0]
car.storage = storage
car.perchasedYear = year
car.save()
return car
if __name__ =='__main__':
print('Starting population...')
populate()
For the population code, I have try two way but got the same message. One is put all the category and model in a list as object, the other one is using string to create new object directly.
In the end this is the whole message feedback:
Traceback (most recent call last):
File "DBPopulation.py", line 77, in <module>
populate()
File "DBPopulation.py", line 45, in populate
Add_CarStorage(ID, category, model, storage, year)
File "DBPopulation.py", line 69, in Add_CarStorage
model = model,)[0]
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/query.py", line 471, in get_or_create
return self.get(**lookup), False
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/query.py", line 376, in get
clone = self.filter(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/query.py", line 794, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/query.py", line 812, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1227, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1253, in _add_q
allow_joins=allow_joins, split_subq=split_subq,
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1183, in build_filter
condition = lookup_class(lhs, value)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/lookups.py", line 19, in __init__
self.rhs = self.get_prep_lookup()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/fields/related_lookups.py", line 100, in get_prep_lookup
self.rhs = target_field.get_prep_value(self.rhs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 946, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: 'Wing'
TXT file:
2512-305,Wing,Nissan UD,10,2003
2010-41,Wing,Hino,4,1997
2607-312,Cargo Drop Side,Isuzu,6,2012
2411-341,Cargo Drop Side,Nissan UD,7,2000
2406-326,Tractor,Nissan UD,12,1996
2211-101,Other,Komatsu,1,0
I understand there might have problems like putting string or nothing in the integer field, but I have reread many time and still not figure out.