Switching to django funcitons rather than raw sql