Skip to content

Commit bfdbfc3

Browse files
committed
Fix auto process
1 parent 425eebd commit bfdbfc3

2 files changed

Lines changed: 19 additions & 9 deletions

File tree

‎test/test_orm.py‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,15 @@ def test_3():
116116
>>> x = a1.save()
117117
>>> Test.get(1)
118118
<Test {'username':u'user','year':0,'id':1}>
119+
>>> b2 = Test1.get(Test1.c.name == 'user')
120+
>>> b2
121+
<Test1 {'test1':<ReferenceProperty:1>,'test2':<ReferenceProperty:1>,'name':u'user','id':1}>
122+
>>> b2.test1 = None
123+
>>> b2.save()
124+
True
125+
>>> b3 = Test1.get(Test1.c.name == 'user')
126+
>>> b3
127+
<Test1 {'test1':None,'test2':<ReferenceProperty:1>,'name':u'user','id':1}>
119128
"""
120129

121130
#testing many2one using collection_name
@@ -636,8 +645,8 @@ def test_save():
636645
... username = Field(CHAR, max_length=20)
637646
... year = Field(datetime.datetime, auto_now_add=True, auto_now=True)
638647
>>> a = Test(username='limodou')
639-
>>> a._get_data()
640-
{'username': u'limodou'}
648+
>>> a._get_data() # doctest:+ELLIPSIS
649+
{'username': u'limodou', 'year': datetime.datetime(...)}
641650
>>> a.save()
642651
True
643652
>>> a.to_dict() # doctest:+ELLIPSIS

‎uliweb/orm/__init__.py‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2339,8 +2339,11 @@ def _get_data(self):
23392339
x = v.get_value_for_datastore(self)
23402340
if isinstance(x, Model):
23412341
x = x.id
2342-
elif x is None and (v.auto_add or (not v.auto_add and not v.auto)):
2343-
x = v.default_value()
2342+
elif x is None:
2343+
if isinstance(v, DateTimeProperty) and v.auto_now_add:
2344+
x = v.now()
2345+
elif (v.auto_add or (not v.auto and not v.auto_add)):
2346+
x = v.default_value()
23442347
else:
23452348
x = v.get_value_for_datastore(self, cached=True)
23462349
if x is not None:
@@ -2357,8 +2360,6 @@ def _get_data(self):
23572360
#todo If need to support ManyToMany and Reference except id field?
23582361
if isinstance(x, Model):
23592362
x = x.id
2360-
elif x is None and (v.auto or (not v.auto_add and not v.auto)):
2361-
x = v.default_value()
23622363
else:
23632364
x = v.get_value_for_datastore(self, cached=True)
23642365
if t != self.field_str(x):
@@ -2419,7 +2420,7 @@ def put(self, insert=False, connection=None):
24192420
setattr(self, 'id', obj.inserted_primary_key[0])
24202421

24212422
if _manytomany:
2422-
for k, v in _manytomany.iteritems():
2423+
for k, v in _manytomany.items():
24232424
if v:
24242425
saved = getattr(self, k).update(v) or saved
24252426

@@ -2433,7 +2434,7 @@ def put(self, insert=False, connection=None):
24332434
#process auto_now
24342435
_manytomany = {}
24352436
for k, v in self.properties.items():
2436-
if v.property_type == 'compound':
2437+
if v.property_type == 'compound' or k == 'id':
24372438
continue
24382439
if not isinstance(v, ManyToMany):
24392440
if isinstance(v, DateTimeProperty) and v.auto_now and k not in d:
@@ -2449,7 +2450,7 @@ def put(self, insert=False, connection=None):
24492450
if old:
24502451
saved = True
24512452
if _manytomany:
2452-
for k, v in _manytomany.iteritems():
2453+
for k, v in _manytomany.items():
24532454
if v is not None:
24542455
saved = getattr(self, k).update(v) or saved
24552456
if saved:

0 commit comments

Comments
 (0)