我正在尝试将旧 mysql 数据库中的时事通讯订阅导入新的 django 时事通讯应用程序,代码如下所示:
for _ns in CmsModuleNmsList.objects.using('old').all():
ns = Newsletter(pk=_ns.listid, title=_ns.name, slug=slugify(_ns.name), email='email@email.com', sender='Admin')
ns.save()
for _list in CmsModuleNmsListuser.objects.using('old').filter(listid=_ns.listid):
try:
_user = CmsModuleNmsUsers.objects.using('old').get(userid=_list.userid)
user = None
try:
user = User.objects.get(email=_user.email)
except:
print "Cannot find user"
Subscription(newsletter=ns, user=user, subscribed=True, email_field=_user.email, name_field=_user.username, create_date=_user.dateadded).save()
except Exception as e:
transaction.rollback()
print _user.username, 'Error', str(e)
当我运行它时,我会得到这样的异常,这是由这一行引起的Subscription(newsletter=ns, user=user, subscribed=True, email_field=_user.email, name_field=_user.username, create_date=_user.dateadded).save()
,如果我取出,user=user
我不会得到异常。