不要使用Django import-export保存在DB重复的行中

时间:2014-10-26 23:06:00

标签: django django-import-export

我使用django import-export导入xls文件,所有工作正常,现在我需要删除具有相同字符串的行,我的意思是

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

仅在导入第2行和第3行时插入数据库

到现在为止,我有这个:

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

       class Meta:
           model = Proyectos

class ProyectosAdmin(ImportExportModelAdmin):
        resource_class = ProyectosResource

1 个答案:

答案 0 :(得分: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的doc

您可以在before_import函数中执行所需的每个测试,检查外键关系的id ...