[Django]-Django-import-export before_import_row to automatically create object if it does not exist

6👍

What is Category#category field? The name of the Category? A string?

try resetting that field to be a primary key of the created Category:

def before_import_row(self, row, **kwargs):
    name = row.get('category')
    (cat, _created) = Category.objects.get_or_create(name=name)
    row['category'] = cat.id
👤daino3

Leave a comment