Fixed: ListField minus index assignment does not work #1119

Add code to detect '-1' as a integer.
Normalize negative index to regular list index
Added list assignment test case
This commit is contained in:
Gang Li
2015-10-12 10:34:26 -04:00
parent d4f6ef4f1b
commit 5bbfca45fa
2 changed files with 54 additions and 2 deletions

View File

@@ -606,7 +606,9 @@ class BaseDocument(object):
for p in parts:
if isinstance(d, (ObjectId, DBRef)):
break
elif isinstance(d, list) and p.isdigit():
elif isinstance(d, list) and p.lstrip('-').isdigit():
if p[0] == '-':
p = str(len(d)+int(p))
try:
d = d[int(p)]
except IndexError:
@@ -640,7 +642,9 @@ class BaseDocument(object):
parts = path.split('.')
db_field_name = parts.pop()
for p in parts:
if isinstance(d, list) and p.isdigit():
if isinstance(d, list) and p.lstrip('-').isdigit():
if p[0] == '-':
p = str(len(d)+int(p))
d = d[int(p)]
elif (hasattr(d, '__getattribute__') and
not isinstance(d, dict)):