我正在尝试从旧的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
,我就不会得到例外。
答案 0 :(得分:0)
在仔细阅读时事通讯的代码后,我发现这些问题导致了这个问题:
assert ((self.user and not self.email_field) or
(self.email_field and not self.user)), \
_('If user is set, email must be null and vice versa.')
我正在设置用户同时设置电子邮件...
参考https://docs.djangoproject.com/en/dev/ref/unicode/#translated-strings