1

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

1 Answer 1

1

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...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.