无法删除Django中的用户对象

时间:2016-06-03 21:22:38

标签: python django exception

我在我的一个Django视图函数中尝试除了块,它会删除在try块中创建的User对象,如果有任何失败的话。当我尝试删除用户时,我收到此错误消息。

OperationalError: no such table: reversion_revision

同样的问题也发生在Django管理员身上。我遇到的问题是发现此类OperationalError的类似案例发生在其他人身上,我不确定为什么会发生这种情况。我在删除块中删除的所有其他对象没有任何问题。

@csrf_exempt
def signup(request):
    # """Register a new account with a new org."""
    if request.is_ajax():
        if request.method == "POST":
            form = SignUp(requestPost(request))

        if form.is_valid():
            cleaned_data = form.cleaned_data

            email = cleaned_data['email']
            password = cleaned_data['password']
            org_name = cleaned_data['org_name']
            org_username = cleaned_data['org_username']

            if cleaned_data['token']:
                invite_token = cleaned_data['token']
            else:
                invite_token = cleaned_data['invite']

            try:
                account = Account.objects.create(email=email, password=password)
                x = email[:30]
                userExists = User.objects.filter(username=email[:30])
                if not userExists:
                    if len(email) < 30:
                        user = User.objects.create_user(email, email, password)
                    else:
                        email = email[:30]
                        user = User.objects.create_user(email, email, password)

                if invite_token:
                    if ThreadInvite.objects.filter(token=invite_token):
                        invitation = ThreadInvite.objects.get(token=invite_token)
                        thread = Thread.objects.get(id=invitation.thread.id)
                        ThreadMember.objects.create(thread=thread, account=account)
                    else:
                        invitation = OrgInvite.objects.get(token=invite_token)
                        if invitation.used:
                            raise Exception("invitation code is invalid")
                        org = Org.objects.get(id=invitation.org.id)
                        OrgMember.objects.create(org=org, account=account)
                        invitation.used = False
                        invitation.save()
                        add_to_welcome(org_id=org.id, account_id=account.id)

                if org_username and org_name:
                    org = Org.objects.create(name=org_name, username=org_username,
                                             actor=account)
                    OrgMember.objects.create(account=account, org=org)
                    Thread.objects.create(name='welcome', account=account, owner=account, org=org,
                                          purpose='To welcome new members to the team.')
                    add_to_welcome(org_id=org.id, account_id=account.id)

                login(request)

                md = mandrill.Mandrill(settings.MANDRILL_API_KEY)
                t = invite_token.replace(' ', '+')
                url = "https://localhost:8000/verify/{}".format(t)
                message = {
                    'global_merge_vars': [
                        {
                            'name': 'VERIFICATION_URL',
                            'content': url
                        },
                    ],
                    'to': [
                        {
                            'email': 'tim@millcreeksoftware.biz',
                        },
                    ],
                    'subject': 'Welcome to Human Link',
                }
                message['from_name'] = message.get('from_name', 'Humanlink')
                message['from_email'] = message.get('from_email',
                                                    'support@humanlink.co')
                try:
                    md.messages.send_template(
                        template_name='humanlink-welcome', message=message,
                        template_content=[], async=True)
                except mandrill.Error as e:
                    logging.exception(e)
                    raise Exception(e)

                context = {
                    'message': 'ok'
                }

                return composeJsonResponse(200, "", context)

            except Exception, e:
                logging.error(e)
                Account.objects.filter(email=email, password=password).delete()
                User.objects.filter(username=email[:30]).delete()
                Org.objects.filter(name=org_name, username=org_username).delete()

结构

├── account
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── admin.py
│   ├── admin.pyc
│   ├── apps.py
│   ├── forms.py
│   ├── forms.pyc
│   ├── migrations
│   ├── models.py
│   ├── models.pyc
│   ├── tests.py
│   ├── tests.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── views.py
│   └── views.pyc
├── api_helpers.py
├── api_helpers.pyc
├── app
│   └── components
├── bower.json
├── bower_components
│   ├── angular
│   ├── angular-animate
│   ├── angular-bootstrap
│   ├── angular-cookies
│   ├── angular-messages
│   ├── angular-sanitize
│   ├── angular-scroll-glue
│   ├── angular-touch
│   ├── angular-ui-router
│   ├── bootstrap
│   ├── checklist-model
│   ├── font-awesome
│   ├── jquery
│   ├── moment
│   ├── pusher-websocket-iso
│   └── underscore
├── cron_tasks.py
├── gulpfile.js
├── logs
│   └── readme.txt
├── manage.py
├── message
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── admin.py
│   ├── admin.pyc
│   ├── forms.py
│   ├── forms.pyc
│   ├── migrations
│   ├── models.py
│   ├── models.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── views.py
│   └── views.pyc
|
├── org
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── admin.py
│   ├── admin.pyc
│   ├── forms.py
│   ├── forms.pyc
│   ├── migrations
│   ├── models.py
│   ├── models.pyc
│   ├── tests.py
│   ├── tests.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── views.py
│   └── views.pyc
├── package.json
├── readme.txt
├── settings
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── base.py
│   ├── base.pyc
│   ├── cron.py
│   ├── development.py
│   ├── development.pyc
│   └── production.py
├── sqlite3
│   └── local.db
├── stylesheets
│   └── less
├── templates
│   ├── accounts
│   ├── admin
│   ├── dashboard
│   ├── footer.html
│   ├── home
│   ├── layouts
│   ├── nav.html
│   ├── pages
│   ├── settings
│   ├── shared
│   ├── site-alert.html
│   └── site.html
├── third_party
│   ├── classytags
│   ├── dateutil
│   ├── django_pusher
│   ├── itsdangerous.py
│   ├── itsdangerous.pyc
│   ├── mandrill.py
│   ├── mandrill.pyc
│   ├── mptt
│   ├── pusher
│   ├── requests
│   ├── reversion
│   ├── six.py
│   ├── six.pyc
│   ├── suit
│   └── werkzeug
├── utility.pyc
├── webapp
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── static
│   ├── urls.py
│   ├── urls.pyc
│   ├── views.py
│   ├── views.pyc
│   ├── wsgi.py
│   └── wsgi.pyc
└── webapp_admin
    ├── __init__.py
    ├── __init__.pyc
    └── static

2 个答案:

答案 0 :(得分:2)

此处的问题不在您的代码中,而是在您的数据库状态中。您似乎添加了django reversion个应用。这会在您的项目中添加新模型。运行

python manage.py syncdb

或者如果你在1.8 +

python manage.py migrate

<强>更新

如果这对您的第三方应用没有迁移有帮助,您需要先使用

创建它们
python manage.py makemigrations name_3rd_party_app

小心在第三方应用上创建迁移。当您运行makemigrations时,它会在第三方应用包中创建迁移。所以它不会出现在您的代码中。在部署它之后,或者其他用户使用它时,将不会进行此迁移。因此,您需要使用https://docs.djangoproject.com/en/1.9/ref/settings/#migration-modules

为创建的迁移提供自定义路径

然后运行migrate

答案 1 :(得分:0)

尝试运行

./manage.py makemigrations revisions

然后

./manage.py migrate

或者只是删除db文件并重新开始

./manage.py migrate