DateField cannot be imported from Excel sheet containing dates
See original GitHub issueExcel stores dates and times internally as floating point numbers where the integer part is the number of days since 31st December 1899 and the fractional part is the time of day. When loaded into a tablib Dataset these values remain as floating point.
Unfortunately the import_export DateWidget assumes that dates will be represented externally as strings and attempts to processing the floating point values using strptime leading to import errors such as shown below:
Line number: 1 - Column 'manufactured': Enter a valid date.
Traceback (most recent call last):
File ".../lib/python3.4/site-packages/import_export/fields.py", line 57, in clean
value = self.widget.clean(value)
File ".../lib/python3.4/site-packages/import_export/widgets.py", line 113, in clean
raise ValueError("Enter a valid date.")
ValueError: Enter a valid date.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File ".../lib/python3.4/site-packages/import_export/resources.py", line 337, in import_data
self.import_obj(instance, row, real_dry_run)
File ".../lib/python3.4/site-packages/import_export/resources.py", line 203, in import_obj
self.import_field(field, obj, data)
ValueError: Column 'manufactured': Enter a valid date.
A simple fix for importing would be to change clean
to accept float values in addition to parsing strings.
I believe a similar issue exists when exporting dates to Excel: they are currently output as strings which means any attempt to manipulate them as dates within Excel will fail. Simply converting to a float value on output would not be sufficient either as the cell would also need to be formatted as a date or time within Excel to display correctly.
Issue Analytics
- State:
- Created 9 years ago
- Comments:9 (5 by maintainers)
Top GitHub Comments
Having just run into this issue today, it would still be very good to get the code included in
import-export
@bmihelac@rhunwicks I’ve put the code in a gist, with updated clean() and render() signatures to work with latest django-import-export, here: https://gist.github.com/manelclos/5c37646120ec0520a9965df32cc6208a
@bmihelac It would be nice to have this in some compat.py file so people are able to use it directly from import-export without much hassle.