Import data containing title instead of ID in django

I am using django-import-export. It is an extension used to import tables of data to the admin panel using a csv file. I have a model with a foreign key : ProductModel, now if i want to import data i have to supply an ID of the ProductModel in the csv. I want a bypass so I can use title of an object instead of id in the csv

class Item(models.Model):
    title = models.CharField(max_length=100)
    model = models.ForeignKey(ProductModel, ....)

class ProductModel(models.Model):
    title = models.CharField(max_length=100)
    desc = models.Tex.....

You have to declare 'title' as the lookup field in your ForeignKeyWidget definition.

For example:

from import_export import fields, resources
from import_export.widgets import ForeignKeyWidget

class ItemResource(resources.ModelResource):
    model = fields.Field(
        column_name='model',
        attribute='model',
        widget=ForeignKeyWidget(ProductModel, 'title'))

    class Meta:
        fields = ('model',)
Back to Top