Django Import Export Strip White Space & Additional Condition
Я использую плагин Django Import and Export и в моем Admin.py мне нужно решить две проблемы.
- The excel file I'm uploading has white space at the end of the cells, I need to remove that.
- This import is uploading test scores, there is no test id in the import to determine if its a different test. However, there is a test date field. I need to be able to get the date field before the import of each row and analyze it to determine if this is an update of the current test data or make a new record signaling this was a different test. I appreciate the help. Here is my admin.py
class MindResource (resources.ModelResource):
state_province = fields.Field(column_name='state_province', attribute='state_province',widget=ForeignKeyWidget(Student, 'state_province'))
def import_row(self, row, instance_loader, **kwargs):
# OVERRIDE ERROR TO IMPORT RECORDS (STOPS FAILURE OF IMPORT)
import_result = super(MindResource, self).import_row(row, instance_loader, **kwargs)
if import_result.import_type == RowResult.IMPORT_TYPE_ERROR:
# COPIES VALUES TO DISPLAY ON THE REPORT
import_result.diff = [row[val] for val in row]
# ADD COLUMN WITH ERROR MESSAGE
import_result.diff.append('Errors: {}'.format([err.error for err in import_result.errors]))
# CLEAR ERRORS AND MARK TO SKIP
import_result.errors = []
import_result.import_type = RowResult.IMPORT_TYPE_SKIP
return import_result
class Meta:
model = Mind
clean_model_instances = True
import_id_fields = ('state_province',)
fields = ('state_province','test_date','reasoning_verbal','reasoning_spatial','reasoning_abstract','executive_flexibility','executive_working_memory',
'executive_attention','speed_processing','speed_visual_motor','memory_verbal','memory_visual')