# Buggy code
def __filter_fields(self, field_names, obj, many=False):
    """Return only those field_name:field_obj pairs specified by
    ``field_names``.

    :param set field_names: Field names to include in the final
        return dictionary.
    :returns: An dict of field_name:field_obj pairs.
    """
    if obj and many:
        try:  # Homogeneous collection
            # Prefer getitem over iter to prevent breaking serialization
            # of objects for which iter will modify position in the collection
            # e.g. Pymongo cursors
            if hasattr(obj, "__getitem__") and callable(getattr(obj, "__getitem__")):
                try:
                    obj_prototype = obj[0]
                except KeyError:
                    obj_prototype = next(iter(obj))
            else:
                obj_prototype = next(iter(obj))
        except (StopIteration, IndexError):  # Nothing to serialize
            return self.declared_fields
        obj = obj_prototype
    ret = self.dict_class()
    for key in field_names:
        if key in self.declared_fields:
            ret[key] = self.declared_fields[key]
        else:  # Implicit field creation (class Meta 'fields' or 'additional')
            if obj:
                attribute_type = None
                try:
                    if isinstance(obj, Mapping):
                        attribute_type = type(obj[key])
                    else:
                        attribute_type = type(getattr(obj, key))
                except (AttributeError, KeyError) as err:
                    err_type = type(err)
                    raise err_type(
                        '"{0}" is not a valid field for {1}.'.format(key, obj)
                    )
                field_obj = self.TYPE_MAPPING.get(attribute_type, fields.Field)()
            else:  # Object is None
                field_obj = fields.Field()
            # map key -> field (default to Raw)
            ret[key] = field_obj
    return ret


# Traceback (AttributeError)
Traceback (most recent call last):
File "scratch/missing-field-nested-bug.py", line 72, in <module>
result = HostSchema().dump(host)
File "/Users/kai/.virtualenvs/SPEZZI/lib/python2.7/site-packages/marshmallow/schema.py", line 509, in dump
**kwargs
File "/Users/kai/.virtualenvs/SPEZZI/lib/python2.7/site-packages/marshmallow/marshalling.py", line 138, in serialize
index=(index if index_errors else None)
File "/Users/kai/.virtualenvs/SPEZZI/lib/python2.7/site-packages/marshmallow/marshalling.py", line 62, in call_and_store
value = getter_func(data)
File "/Users/kai/.virtualenvs/SPEZZI/lib/python2.7/site-packages/marshmallow/marshalling.py", line 132, in <lambda>
getter = lambda d: field_obj.serialize(attr_name, d, accessor=accessor)
File "/Users/kai/.virtualenvs/SPEZZI/lib/python2.7/site-packages/marshmallow/fields.py", line 252, in serialize
return self._serialize(value, attr, obj)
File "/Users/kai/.virtualenvs/SPEZZI/lib/python2.7/site-packages/marshmallow/fields.py", line 447, in _serialize
schema._update_fields(obj=nested_obj, many=self.many)
File "/Users/kai/.virtualenvs/SPEZZI/lib/python2.7/site-packages/marshmallow/schema.py", line 767, in _update_fields
self.__set_field_attrs(ret)
File "/Users/kai/.virtualenvs/SPEZZI/lib/python2.7/site-packages/marshmallow/schema.py", line 788, in __set_field_attrs
field_obj._add_to_schema(field_name, self)
AttributeError: 'NoneType' object has no attribute '_add_to_schema'

# Fixed code
def __filter_fields(self, field_names, obj, many=False):
    """Return only those field_name:field_obj pairs specified by
    ``field_names``.

    :param set field_names: Field names to include in the final
        return dictionary.
    :returns: An dict of field_name:field_obj pairs.
    """
    if obj and many:
        try:  # Homogeneous collection
            # Prefer getitem over iter to prevent breaking serialization
            # of objects for which iter will modify position in the collection
            # e.g. Pymongo cursors
            if hasattr(obj, "__getitem__") and callable(getattr(obj, "__getitem__")):
                try:
                    obj_prototype = obj[0]
                except KeyError:
                    obj_prototype = next(iter(obj))
            else:
                obj_prototype = next(iter(obj))
        except (StopIteration, IndexError):  # Nothing to serialize
            return dict(
                (k, v) for k, v in self.declared_fields.items() if k in field_names
            )
        obj = obj_prototype
    ret = self.dict_class()
    for key in field_names:
        if key in self.declared_fields:
            ret[key] = self.declared_fields[key]
        else:  # Implicit field creation (class Meta 'fields' or 'additional')
            if obj:
                attribute_type = None
                try:
                    if isinstance(obj, Mapping):
                        attribute_type = type(obj[key])
                    else:
                        attribute_type = type(getattr(obj, key))
                except (AttributeError, KeyError) as err:
                    err_type = type(err)
                    raise err_type(
                        '"{0}" is not a valid field for {1}.'.format(key, obj)
                    )
                field_obj = self.TYPE_MAPPING.get(attribute_type, fields.Field)()
            else:  # Object is None
                field_obj = fields.Field()
            # map key -> field (default to Raw)
            ret[key] = field_obj
    return ret