热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

将foreignKey小部件添加到django-import-export-AddingforeignKeywidgettodjango-import-export

Imtryingtoimportdatatooneofmymodels,butitsfailingbecauseImtryingtouploadthefor

I'm trying to import data to one of my models, but it's failing because I'm trying to upload the foreignKey Id, not the iterated number that import-export creates.

我正在尝试将数据导入到我的某个模型中,但它失败了,因为我正在尝试上传foreignKey Id,而不是import-export创建的迭代数。

models.py

from django.db import models
from import_export import resources


class School(models.Model):
    name = models.CharField(max_length=100)
    slug = models.CharField(max_length=100)
    school_id = models.IntegerField(unique=True)

class Sol(models.Model):
    school_id = models.ForeignKey(School, to_field='school_id')
    name = models.CharField(max_length = 100)
    Al1EOC = models.DecimalField(max_digits=5, decimal_places=2)
    AL2EOC = models.DecimalField(max_digits=5, decimal_places=2)

#Class for django-import-export
class SolResource(resources.ModelResource):
    class Meta:
        model = Sol

My admin.py

from import_export.admin import ImportExportModelAdmin


class SolAdmin(ImportExportModelAdmin):
    list_display = ('name', 'school_id')
    resources_class = SolResource
    pass

admin.site.register(Sol, SolAdmin)

My data.csv

id, name, school_id, Al1EOC, AL2EOC
,'Main st school', 1238008, 12.9, 14.9

When I export the data from the SOL model, I get an iterated number for the school ID. I want the actual School ID - the one that holds the foreignKey relationship. And, subsequently, I need to upload data with that foreignKey number. I know the ForeignKey widget is the way to do it, but I don;t understand how it is implemented.

当我从SOL模型导出数据时,我得到学校ID的迭代编号。我想要实际的学校ID - 持有foreignKey关系的那个。然后,我需要使用该foreignKey号上传数据。我知道ForeignKey小部件就是这样做的,但我不明白它是如何实现的。

3 个解决方案

#1


3  

i think it will help:

我认为它会有所帮助:

class SolResource(resources.ModelResource):
    school_id = fields.Field()

    class Meta:
        # ...

    def dehydrate_school_id(self, sol):
        return sol.school_id.school_id # You can get all fields of related model. This is example.

#2


3  

Use the widget- works great.

使用小部件 - 工作得很好。

school_id = fields.Field(column_name='school_id', attribute='Sol', widget=widgets.ForeignKeyWidget(Sol, 'school_id'))

school_id = fields.Field(column_name ='school_id',attribute ='Sol',widget = widgets.ForeignKeyWidget(Sol,'school_id'))

#3


3  

There is ForeignKeyWidget in the documentation. You can use it here. There are also IntegerWidget and DecimalWidget.

文档中有ForeignKeyWidget。你可以在这里使用它。还有IntegerWidget和DecimalWidget。

from import_export.admin import ImportExportModelAdmin

class SolResource(resources.ModelResource):
    school_id = fields.Field(
        column_name='school_id',
        attribute='school_id',
        widget=ForeignKeyWidget(School, 'name'))

    class Meta:
        model = Sol

class SolAdmin(ImportExportModelAdmin):
    list_display = ('name', 'school_id')
    resources_class = SolResource

admin.site.register(Sol, SolAdmin)

This is a working example. Hope it will help.

这是一个有效的例子。希望它会有所帮助。


推荐阅读
author-avatar
披着狼皮的卷毛羊万_784
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有