1
我嘗試運行此代碼:與管理功能複製元素
# -*- coding: utf-8 -*-
from django.core.management.base import BaseCommand
class Command(BaseCommand):
def handle(self, *args, **options):
from blogs.models import Blog, Post
from sitename.settings import DEFAULT_CHARSET
for blog in Blog.objects.all().using('old'):
try:
Blog.objects.get(old_id=blog.id)
continue
except:
pass
new_blog = Blog(
name = blog.name,
description=blog.description,
old_id = blog.id
)
new_blog.save()
但我有例外:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not in range(128)
我GOOGLE了這個問題,並發現此解決方案:
name = blog.name.encode('ascii','ignore')
結果令人不安:所有俄羅斯符號已被刪除。
"Пост номер 15-14" => "15-14"
我該如何正確地複製數據?