Migrated db operations from raw sql to django ORM.