def _get_factory_attr(factory, attr):
    """
    Try getting a meta attribute 'attr' from a factory.

    The attribute is looked up as '_attr' on the factory, then, if the factory
    and its model class names match, as 'attr' on the model's meta. The
    factory's own meta cannot define custom attributes and is skipped.

    If the attribute is not found in either place, an AttributeError is raised.
    """

    try:
        return getattr(factory, "_" + attr)
    except AttributeError:
        # pylint:disable=protected-access
        if factory.__name__ == factory._meta.model.__name__ + "Factory":
            return getattr(factory._meta.model._meta, attr)
        else:
            raise