def set_object_attribute(obj, key, value):
    field_type = None

    try:
        field_type, _, _, _ = obj._meta.get_field_by_name(key)
    except FieldDoesNotExist:
        raise FieldDoesNotExist("Error in fixed field name in "
                                "get_fixed_fields. Field %s "
                                "doesn't exist." % key)

    if isinstance(field_type, ForeignKey):
        if not value:
            # explicit None for empty strings
            value = None
        else:
            model = field_type.related_field.model
            try:
                value = model.objects.get(name=value)
            except model.DoesNotExist:
                raise ValidationError({key: wrap_error(value)})

    elif field_type.choices:
        try:
            value = get_value_from_choices(value, field_type.choices)
        except __HOLE__:
            pass  # Delegate validation to the model

    setattr(obj, key, value)
dataset/ETHPy150Open NeuroVault/NeuroVault/neurovault/apps/statmaps/image_metadata.py/set_object_attribute