I have a Django webapp in which users register, login, logout as usual.
When a user "Deletes" their account, I don't actually want to delete it (because it has foreign keys pointing to it). I just want to mark the User.is_active = False
. I also would like to remove the email address (but not the username) so that the person can create a new account with the same email address. How to do it?
As you can see below, when I try to remove the email address from a User
, it disallows me from doing so. If this operation really is not allowed, then how do other people solve this problem of wanting to allow deleted users to re-register with the same email address? I guess I could just change the email address to some dummy value like [email protected]
, but that seems really ugly.
>>> from django.contrib.auth.models import User
>>> x = User.objects.get(username="someuser")
>>> x.email = None
>>> x.save()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "MyVirtualEnv/lib/python2.7/site-packages/django/db/models/base.py", line 591, in save
force_update=force_update, update_fields=update_fields)
File "MyVirtualEnv/lib/python2.7/site-packages/django/db/models/base.py", line 619, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "MyVirtualEnv/lib/python2.7/site-packages/django/db/models/base.py", line 681, in _save_table
forced_update)
File "MyVirtualEnv/lib/python2.7/site-packages/django/db/models/base.py", line 725, in _do_update
return filtered._update(values) > 0
File "MyVirtualEnv/lib/python2.7/site-packages/django/db/models/query.py", line 600, in _update
return query.get_compiler(self.db).execute_sql(CURSOR)
File "MyVirtualEnv/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 1004, in execute_sql
cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
File "MyVirtualEnv/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 786, in execute_sql
cursor.execute(sql, params)
File "MyVirtualEnv/lib/python2.7/site-packages/django/db/backends/utils.py", line 81, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "MyVirtualEnv/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "MyVirtualEnv/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 128, in execute
return self.cursor.execute(query, args)
File "MyVirtualEnv/lib/python2.7/site-packages/MySQLdb/cursors.py", line 207, in execute
if not self._defer_warnings: self._warning_check()
File "MyVirtualEnv/lib/python2.7/site-packages/MySQLdb/cursors.py", line 117, in _warning_check
warn(w[-1], self.Warning, 3)
Warning: Column 'email' cannot be null
From this question: https://stackoverflow.com/a/29810311/2800876