• Home
  • Django
  • Skip the exception with import export

Skip the exception with import export

linkaiyi
Follow

Skip the Exception row while using django import-export

    def import_row(self, row, instance_loader, **kwargs):
        # overriding import_row to ignore errors and skip rows that fail to import
        # without failing the entire import
        import_result = super(RequestModelResource, self).import_row(row, instance_loader, **kwargs)
        if import_result.import_type == RowResult.IMPORT_TYPE_ERROR:
            # Copy the values to display in the preview report
            import_result.diff = []
            for field in self.get_fields():
                import_result.diff.append(row[field.column_name])
            # import_result.diff = [row[val] for val in row if val != '市名称' or val != '拼音']
            # Add a column with the error message
            import_result.diff.append('Errors: {}'.format([err.error for err in import_result.errors]))
            # clear errors and mark the record to skip
            import_result.errors = []
            import_result.import_type = RowResult.IMPORT_TYPE_SKIP
        return import_result
class Meta in Model Resource
class Meta:
        skip_unchanged = True
        report_skipped = True
        raise_errors = False
        model = MyModel
Object has 0 attachments

Was this article helpful?

This article is viewed 7 times!

Recent Viewed Articles

0 Comments

Leave a Comment

Support