dfrojas
dfrojas

Reputation: 723

Dont save in DB repeated rows with Django import-export

Im importing an xls file with django import-export and all working fine, now i need delete the rows that has the same strings, i mean

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

Only insert in DB when importing the rows 2 and 3

Until now, i have this:

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

       class Meta:
           model = Proyectos

class ProyectosAdmin(ImportExportModelAdmin):
        resource_class = ProyectosResource

Upvotes: 1

Views: 380

Answers (1)

HydrUra
HydrUra

Reputation: 1516

I don't know if it's the right way to do this but I will do this in before_import function:

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

Here is the doc of Tablib to manipulate Datasets !

You can do every test you want in before_import function, checking id for foreignKey relations...

Upvotes: 1

Related Questions