fix complex datetime field invalid string set
This commit is contained in:
parent
b2c3acd025
commit
38703acc29
@ -684,7 +684,10 @@ class ComplexDateTimeField(StringField):
|
|||||||
super(ComplexDateTimeField, self).__set__(instance, value)
|
super(ComplexDateTimeField, self).__set__(instance, value)
|
||||||
value = instance._data[self.name]
|
value = instance._data[self.name]
|
||||||
if value is not None:
|
if value is not None:
|
||||||
instance._data[self.name] = self._convert_from_datetime(value)
|
if isinstance(value, datetime.datetime):
|
||||||
|
instance._data[self.name] = self._convert_from_datetime(value)
|
||||||
|
else:
|
||||||
|
instance._data[self.name] = value
|
||||||
|
|
||||||
def validate(self, value):
|
def validate(self, value):
|
||||||
value = self.to_python(value)
|
value = self.to_python(value)
|
||||||
|
@ -4,6 +4,8 @@ import itertools
|
|||||||
import math
|
import math
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
from mongoengine import *
|
from mongoengine import *
|
||||||
|
|
||||||
from tests.utils import MongoDBTestCase
|
from tests.utils import MongoDBTestCase
|
||||||
@ -191,3 +193,18 @@ class ComplexDateTimeFieldTest(MongoDBTestCase):
|
|||||||
|
|
||||||
fetched_log = Log.objects.with_id(log.id)
|
fetched_log = Log.objects.with_id(log.id)
|
||||||
assert fetched_log.timestamp >= NOW
|
assert fetched_log.timestamp >= NOW
|
||||||
|
|
||||||
|
def test_setting_bad_value_does_not_raise_unless_validate_is_called(self):
|
||||||
|
# test regression of #2253
|
||||||
|
|
||||||
|
class Log(Document):
|
||||||
|
timestamp = ComplexDateTimeField()
|
||||||
|
|
||||||
|
Log.drop_collection()
|
||||||
|
|
||||||
|
log = Log(timestamp="garbage")
|
||||||
|
with pytest.raises(ValidationError):
|
||||||
|
log.validate()
|
||||||
|
|
||||||
|
with pytest.raises(ValidationError):
|
||||||
|
log.save()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user