1

我正在使用 Django 1.5。我有以下模型:

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 身份验证:

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
4

1 回答 1

1

与普通的 python 继承不同,Django 中不允许字段名称“隐藏”

只需使用抽象用户提供的密码字段并在保存方法或表单/api中进行任何自定义保存/检查

阅读此处了解更多信息:

https://docs.djangoproject.com/en/1.8/topics/db/models/#field-name-hiding-is-not-permitted

于 2015-08-26T14:45:45.640 回答