Venugopal srinivasan
Venugopal srinivasan

Reputation: 145

How to import excel files in django that has a foreign key?

I am trying to import excel file through django admin(Import-export). I have two tables. Brand master and Item master. I am using Brand Master as a foreign key in Item master. When I upload brand master file, it is uploading. But when I try to import Item master there is a problem Below is the model.

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

This is the admin.py file.

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)

When I try to import excel file for "ItemMaster" table it is showing Brand_Master_id cannot be empty. Admin Also in my DB, a column named Brand_master_id is created for itemmaster table. Pls help to resolve this

DB thanks

Upvotes: 1

Views: 1307

Answers (1)

Matthew Hegarty
Matthew Hegarty

Reputation: 4231

When importing ItemMaster data, you need to provide the foreign key in the csv which can link to the related BrandMaster instance.

To do this, use ForeignKeyWidget.

For example:

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))

This will then use the value in the Brand_Master csv column to do a pk lookup against the BrandMaster instance (you can use other fields if the csv doesn't provide pk).

I've made some assumptions about your data model here, but hopefully it helps you get to the source of the problem. As always with django-import-export, reviewing the source helps a lot.

Upvotes: 2

Related Questions