簡體   English   中英

無法在Django中刪除用戶對象

[英]Unable to delete User objects in Django

我的Django視圖功能之一中有一個tryexcept塊,如果有任何失敗,它將刪除在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

這里的問題不在您的代碼中,而是在數據庫狀態中。 好像您添加了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

嘗試跑步

./manage.py makemigrations revisions

然后

./manage.py migrate

或者只是刪除db文件並重新開始

./manage.py migrate

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM