"""
需求:两个结构相同的表A, B,需要从 A表 将数据迁移到 B表
"""
from peewee
import *
from playhouse.shortcuts
import model_to_dict
from conf
import BaseModel
class AModel(BaseModel):
name = CharField(default=
"")
age = IntegerField(default=
0)
class Meta:
db_table =
'a_table'
class BModel(BaseModel):
name = CharField(default=
"")
age = IntegerField(default=
0)
class Meta:
db_table =
'b_table'
if __name__ ==
'__main__':
for page
in range(
1,
50):
rows = AModel.select().paginate(page=page, paginate_by=
1000)
lst = []
for row
in rows:
dct = model_to_dict(row)
dct.pop(
"id")
ret = BModel.filter(BModel.md5 == dct[
"md5"]).first()
if ret
is None:
lst.append(dct)
if len(lst) >
0:
BModel.insert_many(lst).execute()
print "数据拷贝完成",
"取出数据:%d" % len(rows),
"成功插入%d" % len(lst)
print "数据迁移完成"
关于model_to_dict的用法,参考: Python编程:playhouse模块转peewee的model对象为字典dict