1

我用 django import-export 导入一个 xls 文件并且一切正常,现在我需要删除具有相同字符串的行,我的意思是

id - name
1  - Jhon
2  - Jhon
3  - Peter

仅在导入第 2 行和第 3 行时插入 DB

直到现在,我有这个:

class ProyectosResource(resources.ModelResource):
       #repeated_rows = fields.Field()

       class Meta:
           model = Proyectos

class ProyectosAdmin(ImportExportModelAdmin):
        resource_class = ProyectosResource
4

1 回答 1

1

我不知道这是否是正确的方法,但我会在 before_import 函数中这样做:

class ProyectosResource(resources.ModelResource):
       #repeated_rows = fields.Field()

       class Meta:
           model = Proyectos

def before_import(self, dataset, dry_run):
        """
        Make standard corrections to the dataset before displaying to user
        """
        list = []
        i = 0
        last = dataset.height - 1
        while i <= last:
            #adding each name to a list
            if ("the name is not in the list (take care about capitalizes letters)"):
                dataset.rpush((dataset.get_col(0)[0], dataset.get_col(1)[0]))  # add this line to the bottom  
                list = list.append(dataset.get_col(1)[0])  # add the name to the list
                dataset.lpop()  # erase it from the top (first line)
            else : 
                #the name is already in the list
                dataset.lpop()  # simply erase it (first line) from the dataset
            i = i + 1

这是Tablib 操作数据集的文档!

你可以在 before_import 函数中做你想做的每一个测试,检查外键关系的 id ......

于 2015-03-16T11:04:56.040 回答