python - 如何在 django 的用户表中重命名 is_active?
问题描述
这是我的用户模型:
class User(AbstractUser):
id = models.AutoField(
verbose_name="ID", serialize=False, auto_created=True, primary_key=True
)
email = models.EmailField(_("email address"), max_length=255, unique=True)
avatar = models.CharField(_("avatar"), max_length=128, default="")
username = None
EMAIL_FIELD = "email"
USERNAME_FIELD = "email"
REQUIRED_FIELDS = []
objects = UserManager()
def __str__(self):
return self.email
现在,我需要重命名is_active
为active
. 本来想取消is_active之类的username,然后新建一个active字段
is_active = None
active = models.BooleanField(_("active"), default=False)
这似乎不起作用,我收到一条错误消息
(admin.E116) The value of 'list_filter[2]' refers to 'is_active', which does not refer to a Field.
解决方案
我不建议删除或重命名内置字段,因为您无疑会收到更多类似于您已经看到的错误消息。如果您不打算使用它,您可以将其默认为某个值(例如,username
字段可以默认为电子邮件地址)。
如果要引用已存在的字段,还可以创建属性字段。这可能适用于您的active
领域。is_active
这种方法的唯一问题是,如果您想更改 的值,您仍然需要更新active
:
class User(AbstractUser):
...
def __str__(self):
return self.email
@property
def active(self):
return self.is_active
推荐阅读
- visual-studio-code - 谷歌云外壳 .customize_environment 不工作
- flutter - Flutter 在文本字段中显示值
- sql - 有没有连接多个表的最佳方法
- unity3d - Unity 2d Animation Controller,空闲动画问题
- c - WINAPI 确定是否存在特定的 SCSI 设备
- api - 使用 POST API 调用重试 Ansible URI PUT API 调用
- python - 修剪二维数组中数据点之间的循环
- python - Django从同一查询集中返回对象列表和ID列表
- apache-spark - 如何将 Spark-excel 添加到 PySpark
- c++ - EventEmitter2 C++ 实现