Django-import-export: Prevent new items. Update only.

Created on 21 Oct 2019  路  2Comments  路  Source: django-import-export/django-import-export

Is there any setting that will allow me to ignore any new items. I would only want to import to update. But let's say that there is a new ID that does not currently exists in the database, I would want to ignore that.

Most helpful comment

I was able to find a solution using the method skip_row

To only update existing items while ignoring any new item I use:

# Do not import any new items. Only update records
def skip_row(self, instance, original):
    if original.id:
        return False
    return super(BookResource, self).skip_row(instance, original)

To import only new items while preventing updates you can use:

# Only import new items. Do not update any record
def skip_row(self, instance, original):
    if not original.id:
        return False
    return True

This assumes your import_id_fields = ('id',) and your resource is called BookResource

All 2 comments

I'm interested in the opposite settings. I'd like to import only new items and to prevent updates.

I was able to find a solution using the method skip_row

To only update existing items while ignoring any new item I use:

# Do not import any new items. Only update records
def skip_row(self, instance, original):
    if original.id:
        return False
    return super(BookResource, self).skip_row(instance, original)

To import only new items while preventing updates you can use:

# Only import new items. Do not update any record
def skip_row(self, instance, original):
    if not original.id:
        return False
    return True

This assumes your import_id_fields = ('id',) and your resource is called BookResource

Was this page helpful?
0 / 5 - 0 ratings