import os import MySQLdb from MySQLdb.cursors import DictCursor from django.core.management.base import BaseCommand from accounts.models import User def create_new_user(data): email = data['email'] firstname = data['firstname'] lastname = data['lastname'] position = data['position'] web_page = data['web_page'] fb = data['fb'] li = data['li'] sk = data['sk'] about = data['about'] password = data['password'] url = data['url'] if not url: url = str(data['id']) user = User(username=email, first_name=firstname, last_name=lastname, email=email, is_staff=False, is_active=True, is_superuser=False, password=password, position=position, url=url) try: user.save() except: return profile = user.profile profile.web_page = web_page profile.facebook = fb profile.linkedin = li profile.skype = sk profile.about = about try: profile.save() except: pass return class Command(BaseCommand): def handle(self, *args, **options): db = MySQLdb.connect(host="localhost", user="expomap", passwd="7FbLtAGjse", db="old_db", charset='utf8', cursorclass=DictCursor) cursor = db.cursor() sql = """ SELECT customers_id as id, customers_email_address as email, customers_password as password, customers_firstname as firstname , customers_lastname as lastname , customers_telephone as phone, customers_job as `position`, customers_web as web_page, customers_facebook as fb, customers_linkedin as li, customers_skype as sk, customers_about as about, url FROM `customers` where customers_email_address!='' """ cursor.execute(sql) result = cursor.fetchall() #user.password = result[0]['customers_password'] for res in result: email = res['email'] print(email) try: user = User.objects.get(username=email) except User.DoesNotExist: user = None create_new_user(res) if user: password = res['password'] user.password = password user.save()