1

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

正如您在下面看到的,当我尝试从 a 中删除电子邮件地址时User,它不允许我这样做。如果这个操作真的不被允许,那么其他人如何解决这个想要让被删除的用户重新注册到同一个邮箱的问题呢?我想我可以将电子邮件地址更改为一些虚拟值,例如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
4

1 回答 1

2

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

在 Django 中,用户定义如下:

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

因此,用户的电子邮件可以为空(即"")但不能为空(即None)。您使用以下内容清除用户的电子邮件:

user.email = ""
user.save()
于 2015-04-22T22:49:00.853 回答