FieldError:类'User'中的本地字段'password'与基类'AbstractBaseUser'中名称相似的字段发生冲突? [英] FieldError: Local field 'password' in class 'User' clashes with field of similar name from base class 'AbstractBaseUser'?
问题描述
我正在使用Django 1.5.我有以下模型:
I am using Django 1.5. I have the following model:
class User(AbstractBaseUser):
#id = models.IntegerField(primary_key=True)
#identifier = models.CharField(max_length=40, unique=True, db_index=True)
username = models.CharField(max_length=90, unique=True, db_index=True)
create_time = models.DateTimeField(null=True, blank=True)
update_time = models.DateTimeField(null=True, blank=True)
email = models.CharField(max_length=225)
password = models.CharField(max_length=120)
external = models.IntegerField(null=True, blank=True)
deleted = models.IntegerField(null=True, blank=True)
purged = models.IntegerField(null=True, blank=True)
form_values_id = models.IntegerField(null=True, blank=True)
disk_usage = models.DecimalField(null=True, max_digits=16, decimal_places=0, blank=True)
objects = UserManager()
USERNAME_FIELD = 'email'
class Meta:
db_table = u'galaxy_user'
运行./manage.py syncdb
时出现此错误:
FieldError: Local field 'password' in class 'User' clashes with field of similar name from base class 'AbstractBaseUser'
我尝试从模型中删除密码字段,但是即使从模型中删除了密码字段,它也没有进行身份验证. 我正在使用我的自定义Django身份验证:
I tried removing the password field from the model but it is not authenticating even if the password field is removed from the model. I am using my custom Django authentication:
class AuthBackend:
def authenticate(self, username=None, password=None):
if '@' in username:
kwargs = {'email': username}
else:
kwargs = {'username': username}
try:
user = User.objects.get(**kwargs)
if user.check_password(password):
return user
except User.DoesNotExist:
return None
def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None
推荐答案
与普通的python继承不同,Django中不允许使用字段名称"hide"(隐藏)
Field name "hiding" is not permitted in Django unlike with normal python inheritance
只需使用抽象用户提供的密码字段,然后在save方法或表单/api中进行任何自定义保存/检查
just use the password field provided by the abstract user and do any custom saving/checks in the save method or in a form/api
在此处阅读有关此内容的更多信息:
read here for more info on this:
https://docs.djangoproject.com/zh-CN/1.8/topics/db/models/#field-name-hiding-is-not-permitted
这篇关于FieldError:类'User'中的本地字段'password'与基类'AbstractBaseUser'中名称相似的字段发生冲突?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!