Django导入/导出:ForeignKey字段返回None

时间:2017-07-19 12:34:14

标签: python django django-import-export

使用Django Import / Export,我在将外键字段导入Django模型时遇到问题。具体来说,我在模型中有三个要导入的字段(第四个参与者 - 具有默认值,因此我们可以忽略该字段),其中一个是IntegerField,另外两个是{ {1}}字段。

导入前,所有三个字段都将转换为数字格式。但是,ForeignKey字段无法读取这些值,而是导入ForeignKey(可能是因为我已将这些模型字段设置为NaN

我在类似问题上尝试了多种解决方案,但他们没有解决这个问题。

我在这里做错了什么?

models.py

null=True

admin.py

class User(auth.models.User,auth.models.PermissionsMixin):
    def __str__(self):
        return "@{}".format(self.username)

class Forskningsresultater(models.Model):
    id = models.IntegerField(primary_key=True)
    authors = models.ManyToManyField(settings.AUTH_USER_MODEL, through='PersonRes', blank=True,related_name='authors',verbose_name="Authors")
    ... (plus irrelevant fields for this question)

class PersonRes(models.Model):
    id = models.IntegerField(primary_key=True)
    user = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,null=True,verbose_name="user")
    forskningsresultater = models.ForeignKey(Forskningsresultater,on_delete=models.CASCADE, null=True,verbose_name="forskningsresultater")
    participant = models.BooleanField(default=True)
    def __str__(self):
        return "{}. Result {}: {}".format(self.id,self.forskningsresultater, self.user)

myscript.py

from import_export import widgets, fields, resources
class PersonResResource(resources.ModelResource):
    user = fields.Field(column_name='user_id', attribute='user', widget=widgets.ForeignKeyWidget(settings.AUTH_USER_MODEL, 'id'))
    forskningsresultater = fields.Field(column_name='forskningsresultater_id', attribute='forskningsresultater', widget=widgets.ForeignKeyWidget(Forskningsresultater, 'id'))
    class Meta:
        model = PersonRes
        fields = ('id','user','forskningsresultater','participant',)

from import_export.admin import ImportExportModelAdmin
class PersonResAdmin(ImportExportModelAdmin):
    class Meta:
        resource_class: PersonResResource
admin.site.register(PersonRes,PersonResAdmin)

打印... df_persres[['A']] = df_persres[['A']].apply(pd.to_numeric) df_persres[['B']] = df_persres[['B']].apply(pd.to_numeric) df_persres[['C']] = df_persres[['C']].apply(pd.to_numeric) df_persres.columns =['id','user_id','forskningsresultater_id'] if( len(df_persres.index) != 0): import tablib from import_export import resources from accounts.models import PersonRes personres_resource = resources.modelresource_factory(model=PersonRes)() antall = len(df_persres.index) for row in range(antall): dataset = tablib.Dataset([ df_persres.iat[row,0], df_persres.iat[row,1], df_persres.iat[row,2]], headers=['id', 'user_id', 'forskningsresultater_id']) result = personres_resource.import_data(dataset, dry_run=True) else: pass 时,我得到以下内容(仅显示dataset的一个实例):

dataset

然而,当我使用id |user_id|forskningsresultater_id ---------|-------|----------------------- 148097456|1480974|56 打印模型时,我得到:

pd.DataFrame.from_records(PersonRes.objects.all().values())

1 个答案:

答案 0 :(得分:0)

列名应如下(删除_id):

user = fields.Field(column_name='user', attribute='user', widget=widgets.ForeignKeyWidget(settings.AUTH_USER_MODEL, 'id'))
forskningsresultater = fields.Field(column_name='forskningsresultater', attribute='forskningsresultater', widget=widgets.ForeignKeyWidget(Forskningsresultater, 'id'))