3

我正在使用django-import-export将 csv 文件加载到迁移文件中,据我所知,这是 Django 1.7 加载初始数据的当前最佳实践。这适用于第一个文件:

class Country(models.Model):

    ISO2 = models.CharField(max_length=2, primary_key=True)
    name = models.CharField(max_length=50, unique=True)

以及整个迁移文件内容。请注意,ISO2 是主键,因此需要添加行 import_id_fields = ['ISO2']。代码改编自这个问题的答案:Loading initial data with Django 1.7 and data migrations

from django.conf import settings
import tablib
from import_export import resources
from web.models import Country
import os
import csv

# load initial data - assume it is all in web/fixtures directory

class CountryResource(resources.ModelResource):

    class Meta:
         model = Country
         import_id_fields = ['ISO2']

fixture_dir = os.path.abspath(os.path.join(settings.PROJECT_ROOT, 'web/fixtures'))
fixture_filename = 'web_country.csv'

def load_fixture(apps, schema_editor):
    fixture_file = os.path.join(fixture_dir, fixture_filename)

    with open(fixture_file, 'r') as content_file:
        content = content_file.read()

    resource = CountryResource()

    data = tablib.Dataset()
    data.csv = content


    result = resource.import_data(data,  dry_run=False,
                                 raise_errors=True)

def unload_fixture(apps, schema_editor):
    "Brutally deleting all entries for this model..."

    country = apps.get_model("web", "Country")
    country.objects.all().delete()

class Migration(migrations.Migration):

    dependencies = [
        ('web', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(load_fixture, reverse_code=unload_fixture),
    ]

现在对于与该文件具有主键关系的下一个文件:

class CountryFootprint(models.Model):
    ISO2 = models.ForeignKey(Country)
    footprint = models.DecimalField(max_digits=18,  decimal_places=6)

和迁移文件的一个子集,我尝试链接外键:

class CountryFootprintResource(resources.ModelResource):

    ISO2_id = fields.Field( widget=widgets.ForeignKeyWidget(Country, 'ISO2'))

    class Meta:
         model = CountryFootprint

这给了我:

  django.db.models.fields.related.RelatedObjectDoesNotExist: CountryFootprint has no ISO2.

也试过:

  ISO2_id = fields.Field(column_name='ISO2_id', attribute='ISO2', widget=widgets.ForeignKeyWidget(Country, 'ISO2'))


  File "/Users/phoebebr/Development/gmd/web/migrations/0003_auto_20141220_1931.py", line 43, in load_fixture
    raise_errors=True)
  File "/Users/phoebebr/.virtualenvs/gmd/lib/python2.7/site-packages/import_export/resources.py", line 359, in import_data
    six.reraise(*sys.exc_info())
  File "/Users/phoebebr/.virtualenvs/gmd/lib/python2.7/site-packages/import_export/resources.py", line 348, in import_data
    row_result.object_repr = force_text(instance)
  File "/Users/phoebebr/.virtualenvs/gmd/lib/python2.7/site-packages/django/utils/encoding.py", line 85, in force_text
    s = six.text_type(s)
TypeError: coercing to Unicode: need string or buffer, Country found

我已经阅读了文档,我确信答案就在那里,但它并没有跳出我的视野!

4

1 回答 1

5

这两条线中的任何一条都有效:

ISO2_id = fields.Field( widget=widgets.ForeignKeyWidget(Country, 'ISO2'))

或者

ISO2_id = fields.Field(column_name='ISO2_id', attribute='ISO2', widget=widgets.ForeignKeyWidget(Country, 'ISO2'))

仅使用:

fields = ('ISO2', 'footprint')

给出错误

django.db.models.fields.related.RelatedObjectDoesNotExist: CountryFootprint has no ISO2.

强制转换为 Unicode 错误是由于我没有从 unicode def 返回的字符串:

def __unicode__(self):
    return self.ISO2

本来应该

def __unicode__(self):
    return self.ISO2.name

睡个好觉解决了这么多编码问题!

于 2014-12-21T10:04:48.400 回答