Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用django导入导出在django迁移中使用外键_Django_Django Import Export - Fatal编程技术网

使用django导入导出在django迁移中使用外键

使用django导入导出在django迁移中使用外键,django,django-import-export,Django,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\u i

我使用在迁移文件中加载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\u id\u fields=['ISO2']。根据对该问题的回答改编的代码::

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

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

我读过这本书,我确信答案就在那里,但它并没有冲着我跳

这两条线中的任何一条都起作用:

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

仅使用:

fields = ('ISO2', 'footprint')
出错

django.db.models.fields.related.RelatedObjectDoesNotExist: CountryFootprint has no ISO2.
强制使用Unicode错误是由于我没有从Unicode定义返回字符串造成的:

def __unicode__(self):
    return self.ISO2
应该是

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

睡个好觉就能解决这么多的编码问题

仅在CountryFootprintResource的Meta类中指定字段=('ISO2')不起作用?尝试过,TypeError:强制使用Unicode:需要字符串或缓冲区,再次找到国家。奇怪的是,你们会认为这个国家是正确的,它正在寻找一条线索。感谢推动刚果民主共和国,最终到达了那里。
def __unicode__(self):
    return self.ISO2
def __unicode__(self):
    return self.ISO2.name