import six


def update_if_changed(obj, values):
    """
    Update fields if changed.
    :param obj: Document instance
    :type obj: Document
    :param values: New values
    :type values: dict
    :returns: List of changed (key, value)
    :rtype: list
    """
    changes = []
    for k, v in six.iteritems(values):
        vv = getattr(obj, k)
        if v != vv:
            if not isinstance(v, int) or not hasattr(vv, "id") or v != vv.id:
                setattr(obj, k, v)
                changes += [(k, v)]
    if changes:
        obj.save()
    return changes