如何在具有外键的django中导入Excel文件?

时间:2019-04-03 12:49:17

标签: python django django-models django-import-export

我正在尝试通过django admin(导入导出)导入Excel文件。我有两张桌子。品牌主管和项目主管。我正在将Brand Master用作Item master中的外键。当我上传品牌主文件时,它正在上传。但是当我尝试导入Item master时出现问题 下面是模型。

from django.db import models

class Sales(models.Model):
    Invoice_Date = models.CharField(max_length = 20, db_column = 'Invoice Date')
    Sales_Value = models.CharField(max_length = 20, db_column = 'Sales Value')

class BrandMaster(models.Model):
    Line_cd = models.CharField(max_length = 10, db_column = 'Line cd')
    Ver_name = models.CharField(max_length = 30, db_column = 'Ver name')
    Brand_man = models.CharField(max_length = 30, db_column = 'Brand man')
    Exec_HR = models.CharField(max_length = 40, db_column = 'Exec HR')
    Cat_man = models.CharField(max_length = 20, db_column = 'Cat man')

    def __str__(self):
            return self.Line_Code

class ItemMaster(models.Model):
    Brand_Master = models.ForeignKey(BrandMaster, on_delete=models.CASCADE)
    Line_Code = models.CharField(max_length = 10, db_column = 'Line cd')
    Item_ID = models.CharField(max_length = 30, db_column = 'Item ID')
    Item_Name = models.CharField(max_length = 30, db_column = 'Item Name')

    def __str__(self):
            return self.Line_Code

这是admin.py文件。

from django.contrib import admin
from .models import Sales,BrandMaster,ItemMaster

from import_export import resources
from import_export import fields
from import_export.admin import ImportExportModelAdmin
from import_export.widgets import ForeignKeyWidget

class SalesResource(resources.ModelResource):
    id = fields.Field(attribute='id', column_name='id')
    Invoice_Date = fields.Field(attribute='Invoice_Date', column_name='Invoice Date')
    Sales_Value = fields.Field(attribute='Sales_Value', column_name='Sales Value')

    class Meta:
        model = Sales

class SalesAdmin(ImportExportModelAdmin):
    resource_class = SalesResource

class BrandMasterResource(resources.ModelResource):
    id = fields.Field(attribute='id', column_name='id')
    Line_cd = fields.Field(attribute='Line_cd', column_name='Line cd')
    Ver_name = fields.Field(attribute='Ver_name', column_name='Ver name')
    Brand_man = fields.Field(attribute='Brand_man', column_name='Brand man')
    Exec_HR = fields.Field(attribute='Exec_HR', column_name='Exec HR')
    Cat_man = fields.Field(attribute='Cat_man', column_name='Cat man')

    class Meta:
        model = BrandMaster

class BrandMasterAdmin(ImportExportModelAdmin):
    resource_class = BrandMasterResource

class ItemMasterResource(resources.ModelResource):
    id = fields.Field(attribute='id', column_name='id')
    Line_cd = fields.Field(attribute='Line_cd', column_name='Line cd')
    Item_ID = fields.Field(attribute='Item_ID', column_name='Item ID')
    Item_Name = fields.Field(attribute='Item_Name', column_name='Item Name')

    class Meta:
        model = ItemMaster

class ItemMasterAdmin(ImportExportModelAdmin):
    resource_class = ItemMasterResource

admin.site.register(Sales, SalesAdmin)
admin.site.register(BrandMaster, BrandMasterAdmin)
admin.site.register(ItemMaster, ItemMasterAdmin)

当我尝试为“ ItemMaster”表导入Excel文件时,它显示Brand_Master_id不能为空。 Admin 同样在我的数据库中,为itemmaster表创建了一个名为Brand_master_id的列。 请帮助解决这个问题

DB 谢谢

1 个答案:

答案 0 :(得分:0)

导入ItemMaster数据时,需要在csv中提供可链接到相关BrandMaster实例的外键。

为此,请使用ForeignKeyWidget

例如:

class ItemMasterResource(resources.ModelResource):
    # your other fields...
    # adjust this to match your model attribute name if necessary
    brand_master = fields.Field(attribute='Brand_Master', column_name='Brand_Master', widget=ForeignKeyWidget(BrandMaster))

然后,它将使用Brand_Master csv列中的值针对BrandMaster实例进行pk查找(如果csv不提供pk,则可以使用其他字段)。

我在这里对您的数据模型进行了一些假设,但希望它可以帮助您找到问题的根源。与django-import-export一样,回顾source很有帮助。