'Recursively save any references and generic references on the
document.'

def cascade_save(self, **kwargs):

    _refs = (kwargs.get('_refs') or [])
    ReferenceField = _import_class('ReferenceField')
    GenericReferenceField = _import_class('GenericReferenceField')
    for (name, cls) in self._fields.items():
        if (not isinstance(cls, (ReferenceField, GenericReferenceField))):
            continue
        ref = self._data.get(name)
        if ((not ref) or isinstance(ref, DBRef)):
            continue
        if (not getattr(ref, '_changed_fields', True)):
            continue
        ref_id = ('%s,%s' % (ref.__class__.__name__, str(ref._data)))
        if (ref and (ref_id not in _refs)):
            _refs.append(ref_id)
            kwargs['_refs'] = _refs
            ref.save(**kwargs)
            ref._changed_fields = []
