【问题标题】:Unable to delete User objects in Django无法删除 Django 中的用户对象
【发布时间】:2016-06-03 21:22:38
【问题描述】:

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

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

【问题讨论】:

  • 除了您提出的实际问题之外,我是否建议您使用transactions 来扭曲您的功能?它旨在准确处理您的情况,在发生错误时回滚更改。

标签: python django exception


【解决方案1】:

这里的问题不在于您的代码,而在于您的数据库状态。看起来您添加了django reversion 应用程序。这会在您的项目中添加新模型。运行

python manage.py syncdb

或者如果你在 1.8+

python manage.py migrate

更新

如果这没有帮助,那么您需要首先创建它们的 3rd 方应用程序没有迁移

python manage.py makemigrations name_3rd_party_app

在 3rd 方应用上创建迁移时要小心。当您运行 makemigrations 时,它会在 3rd-party 应用程序包中创建迁移。所以它不会在你的代码中。并且在您部署它或其他用户使用它之后,就不会有这种迁移。所以你需要为创建的迁移提供自定义路径https://docs.djangoproject.com/en/1.9/ref/settings/#migration-modules

然后运行migrate

【讨论】:

  • 嗯,我运行了 migrate,它说一切都是最新的“没有要应用的迁移。”
  • python manage.py makemigrations reversion 创造了什么?你使用的是什么版本的 django 和 django-reversion?
  • 为还原应用程序进行迁移,而不是进行迁移。我不确定为什么它不会在不指定应用程序的情况下执行“python manage.py makemigrations”,因此它会进行应用程序范围的迁移,但它确实有效。谢谢!!
  • @TimothyJosephBaney 阅读更新。您可能需要了解一些问题。
  • 嗯,所以在我的 MIGRATON_MODULES 字典的设置文件中,我会放 {'reversion': 'reversion.db_migrations'} 吗?
【解决方案2】:

尝试运行

./manage.py makemigrations revisions

然后

./manage.py migrate

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

./manage.py migrate

【讨论】:

    猜你喜欢
    • 2021-12-29
    • 2011-06-23
    • 1970-01-01
    • 2016-05-17
    • 2018-10-17
    • 1970-01-01
    • 2011-03-08
    • 2017-07-04
    • 2015-11-23
    相关资源
    最近更新 更多