'Returns the delta (set, unset) of the changes for a document.
Gets any values that have been explicitly changed.'

def _delta(self):

    doc = self.to_mongo()
    set_fields = self._get_changed_fields()
    unset_data = {}
    parts = []
    if hasattr(self, '_changed_fields'):
        set_data = {}
        for path in set_fields:
            parts = path.split('.')
            d = doc
            new_path = []
            for p in parts:
                if isinstance(d, (ObjectId, DBRef)):
                    break
                elif (isinstance(d, list) and p.lstrip('-').isdigit()):
                    if (p[0] == '-'):
                        p = str((len(d) + int(p)))
                    try:
                        d = d[int(p)]
                    except IndexError:
                        d = None
                elif hasattr(d, 'get'):
                    d = d.get(p)
                new_path.append(p)
            path = '.'.join(new_path)
            set_data[path] = d
    else:
        set_data = doc
        if ('_id' in set_data):
            del set_data['_id']
    for (path, value) in set_data.items():
        if (value or isinstance(value, (numbers.Number, bool))):
            continue
        default = None
        if (self._dynamic and len(parts) and (parts[0] in self._dynamic_fields)):
            del set_data[path]
            unset_data[path] = 1
            continue
        elif (path in self._fields):
            default = self._fields[path].default
        else:
            d = self
            parts = path.split('.')
            db_field_name = parts.pop()
            for p in parts:
                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))):
                    real_path = d._reverse_db_field_map.get(p, p)
                    d = getattr(d, real_path)
                else:
                    d = d.get(p)
            if hasattr(d, '_fields'):
                field_name = d._reverse_db_field_map.get(db_field_name, db_field_name)
                if (field_name in d._fields):
                    default = d._fields.get(field_name).default
                else:
                    default = None
        if (default is not None):
            if callable(default):
                default = default()
        if (default != value):
            continue
        del set_data[path]
        unset_data[path] = 1
    return (set_data, unset_data)
