Timothy Joseph Baney Timothy Joseph Baney - 5 months ago 21
Python Question

Unable to delete User objects in Django

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

Answer

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

Comments