def coerce_single_instance(lookup_field, value):
    """
    Convert from whatever value is given to a scalar value for lookup_field.
    If value is a dict, then lookup_field.name is used to get the value from the dict. Example:
        lookup_field.name = 'id'
        value = {'id': 123, 'name': 'tim'}
        returns = 123
    If value is a model, then lookup_field.name is extracted from the model. Example:
        lookup_field.name = 'id'
        value = <User id=123 name='tim'>
        returns = 123
    Otherwise the value is returned as-is.

    :param lookup_field: Peewee model field used for getting name from value.
    :param value: Some kind of value (usually a dict, Model instance, or scalar).
    """
    if isinstance(value, dict):
        return value.get(lookup_field.name)
    if isinstance(value, peewee.Model):
        return getattr(value, lookup_field.name)
    return value