@@ -582,7 +582,6 @@ def _delta(self):
582
582
583
583
set_fields = self ._get_changed_fields ()
584
584
unset_data = {}
585
- parts = []
586
585
if hasattr (self , '_changed_fields' ):
587
586
set_data = {}
588
587
# Fetch each set item from its path
@@ -592,15 +591,13 @@ def _delta(self):
592
591
new_path = []
593
592
for p in parts :
594
593
if isinstance (d , (ObjectId , DBRef )):
594
+ # Don't dig in the references
595
595
break
596
- elif isinstance (d , list ) and p .lstrip ('-' ).isdigit ():
597
- if p [0 ] == '-' :
598
- p = str (len (d ) + int (p ))
599
- try :
600
- d = d [int (p )]
601
- except IndexError :
602
- d = None
596
+ elif isinstance (d , list ) and p .isdigit ():
597
+ # An item of a list (identified by its index) is updated
598
+ d = d [int (p )]
603
599
elif hasattr (d , 'get' ):
600
+ # dict-like (dict, embedded document)
604
601
d = d .get (p )
605
602
new_path .append (p )
606
603
path = '.' .join (new_path )
@@ -612,26 +609,26 @@ def _delta(self):
612
609
613
610
# Determine if any changed items were actually unset.
614
611
for path , value in set_data .items ():
615
- if value or isinstance (value , (numbers .Number , bool )):
612
+ if value or isinstance (value , (numbers .Number , bool )): # Account for 0 and True that are truthy
616
613
continue
617
614
618
- # If we've set a value that ain't the default value don't unset it.
619
- default = None
615
+ parts = path . split ( '.' )
616
+
620
617
if (self ._dynamic and len (parts ) and parts [0 ] in
621
618
self ._dynamic_fields ):
622
619
del set_data [path ]
623
620
unset_data [path ] = 1
624
621
continue
625
- elif path in self ._fields :
622
+
623
+ # If we've set a value that ain't the default value don't unset it.
624
+ default = None
625
+ if path in self ._fields :
626
626
default = self ._fields [path ].default
627
627
else : # Perform a full lookup for lists / embedded lookups
628
628
d = self
629
- parts = path .split ('.' )
630
629
db_field_name = parts .pop ()
631
630
for p in parts :
632
- if isinstance (d , list ) and p .lstrip ('-' ).isdigit ():
633
- if p [0 ] == '-' :
634
- p = str (len (d ) + int (p ))
631
+ if isinstance (d , list ) and p .isdigit ():
635
632
d = d [int (p )]
636
633
elif (hasattr (d , '__getattribute__' ) and
637
634
not isinstance (d , dict )):
@@ -649,10 +646,9 @@ def _delta(self):
649
646
default = None
650
647
651
648
if default is not None :
652
- if callable (default ):
653
- default = default ()
649
+ default = default () if callable (default ) else default
654
650
655
- if default != value :
651
+ if value != default :
656
652
continue
657
653
658
654
del set_data [path ]
0 commit comments