无法删除Django中的用户对象 [英] Unable to delete User objects in Django

查看:179
本文介绍了无法删除Django中的用户对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  OperationalError:没有这样的表:reversion_revision 

同样的问题也发生在Django管理员中。我发现这个操作错误发生类似的情况发生在其他人的问题,不知道为什么会发生。我删除的所有其他对象在except block中没有任何问题。

  @csrf_exempt 
def注册请求):
#注册一个新的帐户与新的组织。
如果request.is_ajax():
如果request.method ==POST:
form = SignUp(requestPost(request))

如果form.is_valid():
cleaning_data = form.cleaned_data

email = cleaning_data ['email' ]
password = clean_data ['password']
org_name = clean_data ['org_name']
org_username = clean_data ['org_username']

如果clean_data ['token ']:
invite_token = clean_data ['token']
else:
invite_token = clean_data ['invite']

try:
account = Account .objects.create(email = email,password = password)
x =电子邮件[:30]
userExists = User.objects.filter(username = email [:30])
如果不是userExists:
如果len(电子邮件)< 30:
user = User.objects.create_user(email,email,password)
else:
email = email [:30]
user = User.objects.create_user(email ,电子邮件,密码)

如果invite_token:
如果ThreadInvite.objects.filter(token = invite_token):
邀请= ThreadInvite.objects.get(token = invite_token)
thread = Thread.objects.get(id = invitation.thread.id)
ThreadMember.objects.create(thread = thread,account = account)
else:
邀请= OrgInvite。 object.get(token = invite_token)
如果invite.used:
raise异常(邀请代码无效)
org = Org.objects.get(id = invitation.org.id )
OrgMember.objects.creat e(org = org,account = account)
invite.used = False
邀请函()
add_to_welcome(org_id = org.id,account_id = account.id)

如果org_username和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 ='欢迎新成员加入团队。
add_to_welcome(org_id = org.id,account_id = account.id)

登录(请求)

md = mandrill.Mandrill(settings.MANDRILL_API_KEY)
t = invite_token.replace('','+')
url =https:// l ocalhost:8000 / verify / {}。format(t)
message = {
'global_merge_vars':[
{
'name':'VERIFICATION_URL',
'content':url
},
],
'to':[
{
'email':'tim@millcreeksoftware.biz',

',
'subject':'欢迎来到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(
temp如果e =
logging.exception(e)
,则可以使用以下命令:name ='humanlink-welcome',message = message,
template_content = [],async =提升异常(e)

context = {
'message':'ok'
}

return composeJsonResponse(200,,context)

除了异常,e:
logging.error(e)
Account.objects.filter(email = email,password = password).delete()
用户.objects.filter(username = email [:30])。delete()
Org.objects.filter(name = org_name,username = org_username).delete()

结构

 ├──帐户
│├──__init__.py
│├──__init __。pyc
│├──admin.py
│├──admin.pyc
│ ──apps.py
│├──forms.py
│├──forms.pyc
│├──迁移
│├──models.py
│├──models.pyc
│├──tests.py
│├──tests.pyc
│├──urls.py
│├──urls .pyc
│├──views.py
│└──views.pyc
├──api_helpers.py
├──api_helpers.pyc
├─ ─app
│└──组件
├──bower.json
├──bower_components
│├──角
│├──角动画
│├──角度引导
│├──角形曲折
│├──角度消息
│├──角度消毒
│├─ - 角滚动胶
│├──角触点
│├──角u-router
│├──bootstrap
│├──清单模型
│├──font-a wesome
│├──jquery
│├──时刻
│├──pusher-websocket-iso
│└──下划线
├──cron_tasks。 py
├──gulpfile.js
├──日志
│└──readme.txt
├──manage.py
├──消息
│├──__init__.py
│├──__init __。pyc
│├──admin.py
│├──admin.pyc
│├── forms.py
│├──forms.pyc
│├──迁移
│├──models.py
│├──models.pyc
│ ├──urls.py
│├──urls.pyc
│├──views.py
│└──views.pyc
|
├──org
│├──__init__.py
│├──__init __。pyc
│├──admin.py
│├──管理员.pyc
│├──forms.py
│├──forms.pyc
│├──迁移
│├──models.py
│├ ──models.pyc
│├──tests.py
│├──tests.pyc
│├──urls.py
│├──urls.pyc
│├──views.py
│└──views.pyc
├──package.json
├──readme.txt
├──设置
│├──__init__.py
│├──__init __。pyc
│├──base.py
│├──base.pyc
│├─ ─cron.py
│├──development.py
│├──development.pyc
│└──production.py
├──sqlite3
│ └──local.db
├──样式表
│└──少
├──临时lates
│├──帐户
│├──管理
│├──仪表板
│├──footer.html
│├──home
│├──布局
│├──nav.html
│├──页
│├──设置
│├──共享
│ ├──site-alert.html
│└──site.html
├──third_party
│├──classytags
│├──dateutil
│ ├──django_pusher
│├──itsdangerous.py
│├──itsdangerous.pyc
│├──mandrill.py
│├──mandrill.pyc
│├──mptt
│├──推送
│├──请求
│├──回复
│├──six.py
│ ├──six.pyc
│├──西装
│└──werkzeug
├──utility.pyc
├──webapp
│├── __init__.py
│├──__init __。pyc
│├──静态
│├──urls.py
│├──urls.pyc
│├──views.py
│├──views.pyc
│├──wsgi.py
│└──wsgi.pyc
└──webapp_admin
├──__init__。 py
├──__init __。pyc
└──静态


解决方案

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

  python manage.py syncdb 

或者如果你在1.8 +

  python manage.py migrate 

更新



t帮助比没有迁移您的第三方应用程序,您需要首先使用

  python manage.py makemigrations name_3rd_party_app 

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



然后运行 migrate


I have a try except block in one of my Django view functions that will delete a User object that was created in the try block if anything should fail. When I attempt to delete the User I get this error message.

OperationalError: no such table: reversion_revision

The same issue happens in the Django admin as well. I am having issues finding similar cases of this OperationalError happening to other people, and am not sure why it is happening. All the other objects that I am deleting in the except block delete without any issues.

@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()

Structure

├── 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

解决方案

Problem here is not in your code but in your database state. Looks like you added django reversion app. Which adds new Models in your project. Run

python manage.py syncdb

or if you in 1.8+

python manage.py migrate

Update

If this doesn't help than there are no migrations for your 3rd-party app you need to first create them with

python manage.py makemigrations name_3rd_party_app

Be careful with creating migrations on 3rd-party apps. When you run makemigrations it creates migrations in 3rd-party app package. So it won't be in your code. And after you deploy it, or other user uses it there are will not be this migrations. So you need to supply custom path for created migrations with https://docs.djangoproject.com/en/1.9/ref/settings/#migration-modules

And then run migrate

这篇关于无法删除Django中的用户对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆