Python 从迭代器的CSV向django模型分配数据
我正在从CSV读取信息到django模型,但它不断抛出一个Python 从迭代器的CSV向django模型分配数据,python,django,csv,import,Python,Django,Csv,Import,我正在从CSV读取信息到django模型,但它不断抛出一个ValueError:无法分配“'Sheffield United'”:“Match.home\u team”必须是一个“team”实例。我可以在管理界面中很好地添加数据(可能很明显),但尝试以编程方式添加数据会导致错误 我对“League”也有同样的问题,只是为了测试而对其进行了注释——League(“E2”)对象和球队“Sheffield United”都存在于数据库中,因为我添加了它们来测试这一点 然后,我将它们更改为,例如home
ValueError:无法分配“'Sheffield United'”:“Match.home\u team”必须是一个“team”实例。
我可以在管理界面中很好地添加数据(可能很明显),但尝试以编程方式添加数据会导致错误
我对“League”也有同样的问题,只是为了测试而对其进行了注释——League(“E2”)对象和球队“Sheffield United”都存在于数据库中,因为我添加了它们来测试这一点
然后,我将它们更改为,例如home\u team=team.objects.get(id=row[2])
。我认为这可能解决了最初的问题,但我现在得到:ValueError:invalid literal for int(),以10为基数:'Sheffield United'
,这让人困惑,因为它是一个字符串
Models.py:
class League (models.Model):
name = models.CharField(max_length=2)
last_modified = models.CharField(max_length=50)
def __unicode__(self):
return unicode(self.name)
class Team(models.Model):
team_name = models.CharField(max_length=50)
league = models.ForeignKey(League)
team_colour = models.CharField(max_length=6, null=True, blank=True)
def __unicode__(self):
return unicode (self.team_name)
class Match(models.Model):
RESULT_CHOICES = (
('H', 'Home'),
('D', 'Draw'),
('A', 'Away'))
league = models.ForeignKey(League)
match_date = models.DateTimeField()
home_team = models.ForeignKey(Team)
away_team = models.CharField(max_length=50)
full_time_home_goals = models.PositiveSmallIntegerField(blank=True, null=True)
full_time_away_goals = models.PositiveSmallIntegerField(blank=True, null=True)
full_time_result = models.CharField(max_length=1, choices=RESULT_CHOICES, blank=True, null=True)
half_time_home_goals = models.PositiveSmallIntegerField(blank=True, null=True)
half_time_away_goals = models.PositiveSmallIntegerField(blank=True, null=True)
half_time_result = models.CharField(max_length=1, choices=RESULT_CHOICES,blank=True, null=True)
home_shots = models.PositiveSmallIntegerField(blank=True, null=True)
away_shots = models.PositiveSmallIntegerField(blank=True, null=True)
home_shots_on_target = models.PositiveSmallIntegerField(blank=True, null=True)
away_shots_on_target = models.PositiveSmallIntegerField(blank=True, null=True)
home_corners = models.PositiveSmallIntegerField(blank=True, null=True)
away_corners = models.PositiveSmallIntegerField(blank=True, null=True)
home_yellow = models.PositiveSmallIntegerField(blank=True, null=True)
away_yellow = models.PositiveSmallIntegerField(blank=True, null=True)
home_red = models.PositiveSmallIntegerField(blank=True, null=True)
away_red = models.PositiveSmallIntegerField(blank=True, null=True)
def __unicode__(self):
return unicode(self.home_team) + " v " + unicode(self.away_team) + " " + unicode(self.match_date)
class Meta:
verbose_name_plural = "Matches"
我正在使用的管理命令是:(现在我从命令行运行它来调试它,所以我从代码中去掉了BaseCommand子类——它不会影响我看到的错误。)
原始回溯(关于“必须是实例”错误:)
回溯(最近一次呼叫最后一次):
文件“manage.py”,第10行,在
从命令行(sys.argv)执行命令
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site packages/django/core/management/_init__.py”,第453行,从命令行执行
utility.execute()
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site packages/django/core/management/_init__.py”,第392行,在execute中
self.fetch_命令(子命令)。从_argv(self.argv)运行_
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site packages/django/core/management/_init__.py”,第272行,在fetch_命令中
klass=加载命令类(应用程序名称,子命令)
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site packages/django/core/management/_init__.py”,第77行,在load_command_类中
模块=导入模块(“%s.management.commands.%s%”(应用程序名称,名称))
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site packages/django/utils/importlib.py”,导入模块第35行
__导入(名称)
文件“/Users/chris/Dropbox/Django/gmblnew/core/management/commands/importsv.py”,第24行,在
全职结果=第[6]行,
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site packages/django/db/models/base.py”,第403行,在__
setattr(self,field.name,rel_obj)
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site packages/django/db/models/fields/related.py”,第405行,在__
self.field.name,self.field.rel.to.\u meta.object\u name))
ValueError:无法分配“'Sheffield United'”:“Match.home_team”必须是“team”实例。
最近的回溯:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 453, in execute_from_command_line
utility.execute()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 272, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 77, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/Users/chris/Dropbox/Django/gmblnew/core/management/commands/ImportCSV.py", line 14, in <module>
home_team = Team.objects.get(id=row[2]),
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/manager.py", line 143, in get
return self.get_query_set().get(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/query.py", line 379, 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 655, 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 673, 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 1266, in add_q
can_reuse=used_aliases, force_having=force_having)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1197, in add_filter
connector)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/sql/where.py", line 71, in add
value = obj.prepare(lookup_type, value)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/sql/where.py", line 339, in prepare
return self.field.get_prep_lookup(lookup_type, value)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 322, in get_prep_lookup
return self.get_prep_value(value)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 555, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: 'Sheffield United'
回溯(最近一次呼叫最后一次):
文件“manage.py”,第10行,在
从命令行(sys.argv)执行命令
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site packages/django/core/management/_init__.py”,第453行,从命令行执行
utility.execute()
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site packages/django/core/management/_init__.py”,第392行,在execute中
self.fetch_命令(子命令)。从_argv(self.argv)运行_
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site packages/django/core/management/_init__.py”,第272行,在fetch_命令中
klass=加载命令类(应用程序名称,子命令)
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site packages/django/core/management/_init__.py”,第77行,在load_command_类中
模块=导入模块(“%s.management.commands.%s%”(应用程序名称,名称))
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site packages/django/utils/importlib.py”,导入模块第35行
__导入(名称)
文件“/Users/chris/Dropbox/Django/gmblnew/core/management/commands/importsv.py”,第14行,在
home_team=team.objects.get(id=row[2]),
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site packages/django/db/models/manager.py”,get中第143行
返回self.get\u query\u set().get(*args,**kwargs)
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site packages/django/db/models/query.py”,第379行,在get中
clone=self.filter(*args,**kwargs)
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site packages/django/db/models/query.py”,过滤器中第655行
返回self.\u filter\u或\u exclude(False、*args、**kwargs)
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site packages/django/db/models/query.py”,第673行,在“filter”或“exclude”中
clone.query.add_q(q(*args,**kwargs))
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site packages/django/db/models/sql/query.py”,第1266行,在add_q中
can_reuse=使用的_别名,force_having=force_having)
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site packages/django/db/models/sql/query.py”,第1197行,在add_过滤器中
连接器)
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site packages/django/db/models/sql/where.py”,第71行,添加
值=对象准备(查找类型,值)
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site packages/django/db/models/sql/where.py”,第339行,在prepare中
返回self.field.get\u prep\u lookup(lookup\u类型,值)
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site packages/django/db/models/fields/_______________.py”,第322行,在get_prep_查找中
返回self.get_prep_值(value)
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/fields/__-init_____;.py”,第555行,在get_prep_值中
返回int(值)
ValueError:基数为10的int()的文本无效:“Sh”
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 453, in execute_from_command_line
utility.execute()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 272, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 77, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/Users/chris/Dropbox/Django/gmblnew/core/management/commands/ImportCSV.py", line 24, in <module>
full_time_result = row[6],
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/base.py", line 403, in __init__
setattr(self, field.name, rel_obj)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/fields/related.py", line 405, in __set__
self.field.name, self.field.rel.to._meta.object_name))
ValueError: Cannot assign "'Sheffield United'": "Match.home_team" must be a "Team" instance.
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 453, in execute_from_command_line
utility.execute()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 272, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 77, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/Users/chris/Dropbox/Django/gmblnew/core/management/commands/ImportCSV.py", line 14, in <module>
home_team = Team.objects.get(id=row[2]),
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/manager.py", line 143, in get
return self.get_query_set().get(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/query.py", line 379, 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 655, 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 673, 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 1266, in add_q
can_reuse=used_aliases, force_having=force_having)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1197, in add_filter
connector)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/sql/where.py", line 71, in add
value = obj.prepare(lookup_type, value)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/sql/where.py", line 339, in prepare
return self.field.get_prep_lookup(lookup_type, value)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 322, in get_prep_lookup
return self.get_prep_value(value)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 555, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: 'Sheffield United'
ValueError: Cannot assign "'Sheffield United'": "Match.home_team" must be a "Team" instance.
from django.core.management.base import BaseCommand, CommandError
import csv
import csvImporter
from core.models import Match
master_data = open ('/Users/chris/Dropbox/Django/gmblnew/data/testfile.csv', 'r')
data = list(tuple(rec) for rec in csv.reader(master_data, delimiter=','))
from core.models import Match, League, Team
for row in data:
league, _ = League.objects.get_or_create(name=row[0])
home_team, _ = Team.objects.get_or_create(team_name=row[2], league=league)
away_team, _ = Team.objects.get_or_create(team_name=row[3], league=league)
current_match = Match(
league = league,
home_team = home_team,
away_team = away_team,
match_date = row[1],
full_time_home_goals = row[4],
full_time_away_goals = row[5],
home_shots = row[10],
away_shots = row[11],
home_shots_on_target = row[12],
away_shots_on_target = row[13],
home_corners = row[16],
away_corners = row[17],
full_time_result = row[6],
)
print current_match