python - 如何删除 Django 用户的电子邮件?

标签 python django django-contrib

我有一个 Django Web 应用程序,用户可以像往常一样注册、登录、注销。 当用户“删除”他们的帐户时,我实际上并不想删除它(因为它有指向它的外键)。我只想标记 User.is_active = False。我还想删除电子邮件地址(但不是用户名),以便该人可以使用相同的电子邮件地址创建新帐户。怎么做?

正如您在下面看到的,当我尝试从用户中删除电子邮件地址时,它不允许我这样做。如果确实不允许这种操作,那么其他人如何解决这个想要允许已删除的用户使用相同的电子邮件地址重新注册的问题呢?我想我可以将电子邮件地址更改为一些虚拟值,例如 dummy@dummy.net,但这看起来真的很难看。

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

最佳答案

来自这个问题:https://stackoverflow.com/a/29810311/2800876

In Django, a User is defined as follows:

email = models.EmailField(_('email address'), blank=True)

Consequently, a user's email can be blank (i.e. "") but not null (i.e. None). You clear a user's email with the following:

user.email = ""
user.save()

关于python - 如何删除 Django 用户的电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27305137/

相关文章:

Django 1.2 : Multiple database and Generic Content Types

django:来自 django.contrib.auth 的测试失败

python - 我如何获得查询执行时间的 psycopg2 日志记录?

python - 如何限制一名用户投一票?

python - Django:L10N 和 en-IN

python - Django rest 框架验证 POST 请求参数的最佳方式

python - django 评论 : how to prevent form errors from redirecting the user to the preview page?

python 代码在一个文件上有效,但在其他文件上失败

python - Pandas 中的 sort_values() 方法

python - 即使安装了 BeautifulSoap 的 ImportError