# coding=utf-8 from base import init_env init_env(interactive = True) from apps.web.helpers import get_wechat_user_manager_mp_proxy from apps.web.user.models import Card from apps.web.agent.models import Agent agent_product_map = {} error_cards = [] def write_record(): with open("card_product_agent_id_add_record.txt", "wb") as f: for line in error_cards: f.write(line.encode("utf-8")) def get_product_agent_id(agentId): productId = agent_product_map.get(agentId, None) if not productId: agent = Agent.objects.get(id=agentId) productId = get_wechat_user_manager_mp_proxy(agent).occupantId agent_product_map[agentId] = productId return productId def main(): cards = Card.objects.all() for card in cards: if card.agentId: card.productAgentId = get_product_agent_id(card.agentId) else: card.productAgentId = "" error_cards.append("%s\n" % str(card.id)) try: card.save() except Exception as e: print e write_record() if __name__ == '__main__': main()