Fix ImportError and AttributeError in models.py.

Reviewed at https://reviews.reviewboard.org/r/5966/