def get_tag(tag):
    """
    Utility function for accepting single tag input in a flexible
    manner.

    If a ``Tag`` object is given it will be returned as-is; if a
    string or integer are given, they will be used to lookup the
    appropriate ``Tag``.

    If no matching tag can be found, ``None`` will be returned.
    """
    from tagging.models import Tag
    if isinstance(tag, Tag):
        return tag
    try:
        if isinstance(tag, str):
            return Tag.objects.get(name=tag)
        elif isinstance(tag, int):
            return Tag.objects.get(id=tag)
    except Tag.DoesNotExist:
        pass
    return None
