'Prepare a value to be used in a query by resolving it if it is an
expression and otherwise calling the field\'s get_db_prep_save().'

def prepare_value(self, field, value):

    if hasattr(value, 'resolve_expression'):
        value = value.resolve_expression(self.query, allow_joins=False, for_save=True)
        if value.contains_column_references:
            raise ValueError(('Failed    to    insert    expression    "%s"    on    %s.    F()    expressions    can    only    be    used    to    update,    not    to    insert.' % (value, field)))
        if value.contains_aggregate:
            raise FieldError('Aggregate    functions    are    not    allowed    in    this    query')
    else:
        value = field.get_db_prep_save(value, connection=self.connection)
    return value
