0

我正在尝试将旧 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我不会得到异常。

4

1 回答 1

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

于 2013-02-20T00:54:32.373 回答