from django.core.management.base import BaseCommand, CommandError from theme.models import Theme import MySQLdb from MySQLdb.cursors import DictCursor class Command(BaseCommand): def handle(self, *args, **options): db = MySQLdb.connect(host="localhost", user="root", passwd="qazedc", db="expomap_ru", charset='utf8', cursorclass=DictCursor) cursor = db.cursor() sql = """SELECT * FROM categories LEFT JOIN categories_description ON categories.categories_id=categories_description.categories_id """ cursor.execute(sql) res = cursor.fetchall() for th in res: theme = Theme(id=th['categories_id'], url=th['url'], types=0) if th['expo']: theme.types.exposition = True if th['conference']: theme.types.conference = True theme.translate('ru') theme.name = th.get('categories_name', '') theme.main_title = th.get('categories_heading_title', '') theme.description = th.get('categories_description', '') theme.title = th.get('categories_meta_title', '') theme.descriptions = th.get('categories_meta_description', '') theme.keywords = th.get('categories_meta_keywords', '') if theme.name: theme.save() print(theme) print('success')