FieldError: Local field 'password' in class 'User' clashes with field of similar name from base class 'AbstractBaseUser'?

五迷三道 提交于 2019-12-24 14:36:08

问题


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'

I am getting this error when running ./manage.py syncdb:

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

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

回答1:


Field name “hiding” is not permitted in Django unlike with normal python inheritance

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/en/1.8/topics/db/models/#field-name-hiding-is-not-permitted



来源:https://stackoverflow.com/questions/15290750/fielderror-local-field-password-in-class-user-clashes-with-field-of-similar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!