def get_model_at_related_field(model, attr):
    """
    Looks up ``attr`` as a field of ``model`` and returns the related model class.  If ``attr`` is
    not a relationship field, ``ValueError`` is raised.

    """
    field = model._meta.get_field(attr)
    if hasattr(field, 'related_model'):
        return field.related_model
    raise ValueError('{model}.{attr} ({klass}) is not a relationship field.'
        .format(**{'model': model.__name__, 'attr': attr, 'klass': field.
        __class__.__name__}))
