# -*- coding: utf-8 -*- import MySQLdb from MySQLdb.cursors import DictCursor from django.core.management.base import BaseCommand, CommandError from country.models import Country class Command(BaseCommand): def handle(self, *args, **options): # local database(will not work on production) db = MySQLdb.connect(host="localhost", user="root", passwd="qazedc", db="test", charset='utf8', cursorclass=DictCursor) cursor = db.cursor() # !!!database can change sql = "SELECT country_code as code, geo_lat as latitude, geo_lng as longitude FROM test.country_country LEFT JOIN localization.meta_location ON test.country_country.country_code=localization.meta_location.iso COLLATE utf8_unicode_ci WHERE localization.meta_location.type='CO' AND geo_lat IS NOT NULL;" cursor.execute(sql) result = cursor.fetchall() for item in result: updates = Country.objects.filter(country_code=item['code']).update(latitude=item['latitude'], longitude=item['longitude']) if updates: print(item['code'])