def fetch_user(query):
    """Get user by ``pk`` or ``username``. Raise error if it doesn't exist."""
    user_filter = {'pk': query} if query.isdigit() else {'username': query}
    user_model = get_user_model()

    try:
        return user_model.objects.get(**user_filter)
    except user_model.DoesNotExist:
        raise exceptions.ParseError("Unknown user: {}".format(query))
Get user by ``pk`` or ``username``. Raise error if it doesn't exist.