Skip to content

Commit f894987

Browse files
committed
#26 Make EDTFField continue to work with derived DateField
The change to returning `struct_time` objects from the lower/upper strict/fuzzy methods broke the `EDTFField` implementation. This change fixes it so it can write to derived `DateField`s again, though this is still subject to the 1 to 9999 AD year restrictions of Python's `datetime` module so a better fix would be to support or require numeric target fields instead.
1 parent bd0c577 commit f894987

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

edtf/fields.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from edtf import parse_edtf, EDTFObject
99
from edtf.natlang import text_to_edtf
10+
form edtf.utils import struct_time_to_date
1011

1112
DATE_ATTRS = (
1213
'lower_strict',
@@ -116,7 +117,15 @@ def pre_save(self, instance, add):
116117
g = getattr(self, field_attr, None)
117118
if g:
118119
if edtf:
119-
setattr(instance, g, getattr(edtf, attr)())
120+
target_field = getattr(instance, g)
121+
value = getattr(edtf, attr)()
122+
if isinstance(target_field, models.DateField):
123+
value = struct_time_to_date(value)
124+
else:
125+
raise NotImplementedError(
126+
u"EDTFField does not support %s as a derived data"
127+
u" field, only DateField" % type(target_field))
128+
setattr(instance, g, value)
120129
else:
121130
setattr(instance, g, None)
122131
return edtf

0 commit comments

Comments
 (0)