导入功能调用两次后

时间:2019-12-12 13:29:51

标签: python django django-import-export

嗨,我正在导入文件,我想删除不在CSV文件中但在数据库中存在的所有数据,因此每次CSV数据都在数据库中时。旧数据将被删除。

def after_import_instance(self, instance, new, **kwargs):
        """ For each row in the import file, add the pk to the list """
        if instance.pk:
            self.imported_rows_pks.append(instance.pk)

    def after_import(self, dataset, result, using_transactions, dry_run=False, **kwargs):
        """  delete all rows not in the import data set. Then call the same method in the parent to still sequence the DB """
        ids = list(set(self.imported_rows_pks))
        Product.objects.exclude(pk__in=ids).delete()
        import_export.resources.ModelResource.after_import(self, dataset, result, using_transactions, dry_run, **kwargs)

我的代码运行了2次,您能帮我吗?我认为该函数会在确认导入触发时调用一次。

0 个答案:

没有答案