gpt4 book ai didi

python - Django ugettext 与 AbstractUser 字段

转载 作者:行者123 更新时间:2023-12-01 04:58:15 26 4
gpt4 key购买 nike

我使用 ugettext_lazy 来翻译默认的 AbstractBaseUser 字段,我发现了一个问题。我想将名为 password 的字段翻译为我的母语,但我收到的唯一结果是

django.core.exceptions.FieldError: Local field 'password' in class 'User' clashes with field of similar name from base class 'AbstractBaseUser'

这不好。

我的代码如下所示

class User(AbstractBaseUser, PermissionsMixin):
password = models.CharField(_('Parool'), max_length=128)
....

有什么方法可以转换来自 AbstractBaseUser 或任何其他定义的模型的所有字段吗? Django 的文档没有提及我的问题。

有趣的是,我不能只翻译这个字段。

最佳答案

基于doc :

In normal Python class inheritance, it is permissible for a child class to override any attribute from the parent class. In Django, this is not permitted for attributes that are Field instances (at least, not at the moment). If a base class has a field called author, you cannot create another model field called author in any class that inherits from that base class.

从模型中删除或重命名密码字段。

如果您想翻译密码字段,请尝试以下操作:

class User(AbstractBaseUser, PermissionsMixin):
# define your extra fields

AbstractBaseUser._meta.get_field('password').verbose_name = _('Parool')

关于python - Django ugettext 与 AbstractUser 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26874471/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com