CSV から django モデルに情報を読み込んでいますが、ValueError: Cannot assign "'Sheffield United'": "Match.home_team" must be a "Team" instance.
I can add data fine in the admin interface (おそらく明らかに) がスローされ続けますが、プログラムでそれを実行しようとすると、そのエラーが発生します。
「League」にも同じ問題があり、テストのためにコメントアウトしました。これをテストするために追加したため、リーグ (「E2」) オブジェクトとチーム「Sheffield United」の両方がデータベースに存在します。
次に、たとえば この回答home_team = Team.objects.get(id=row[2])
のように変更しました。. これで最初の問題は解決したと思いますが、現在は : を取得していますが、これは文字列であるため不可解です。ValueError: invalid literal for int() with base 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 サブクラスを削除しました。表示されていたエラーには影響しませんでした。)
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
for row in data:
current_match = Match(
league=row[0],
match_date = row[1],
home_team = row[2],
away_team = row[3],
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
元のトレースバック (「インスタンスである必要があります」エラー:)
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'
現時点では、テスト用にいくつかの初期データを読み込んでいますが、データベースへの CSV の操作は定期的に行うものなので、いくつかのガイダンスをいただければ幸いです。(私はいくつかの CSVImporter ツールを見てきました - 今は、自分がしていることの根性を理解したいので、それらを使用したくありません。この問題を乗り越えることができれば十分です。)