Reputation: 1744
I'm usign django-excel
library in my Django project, and I want to skip some rows before save it to the database using the save_to_database()
method.
I have something like the following:
file = form.cleaned_data['file']
file.save_to_database(
model=MyModel,
mapdict = fields,
initializer=self.choice_func,
)
All is working ok but I want to validate the data before call save_to_database
function. The idea to do it is to add the rows that are not valid in an array and return it to notify the user that those fields not were saved.
Upvotes: 1
Views: 214
Reputation: 1744
Finally I achieved this goal returning None
instead of the row in self.choice_fun
function:
This function look like the following:
def choice_fun(self,row):
# Do whatever thing to validate your row
if row[5] != SOME_VALUE:
return None
return row
I also used some global variables to check if some rows had errors, then I returned back that data to the response to give the user a feedback in some way.
Upvotes: 2