gpt4 book ai didi

django 属性错误 : dict object has no attribute 'pk'

转载 作者:行者123 更新时间:2023-12-04 15:50:32 25 4
gpt4 key购买 nike

我正在制作一个简单的 django rest 框架项目。这只是创建一个新用户,然后登录。当我使用 django basic auth 用户模型时,一切正常。但是将基本用户模型更改为自定义用户后,在创建新用户时出现此错误:

dict object has no attribute 'pk'

自定义用户模型引用 django docs .

错误说:

File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/django/contrib/auth/__init__.py",
line 100, in login
if _get_user_session_key(request) != user.pk or ( AttributeError: 'dict' object has no attribute 'pk'

这似乎是说用户模型没有pk,但我不明白。

模型.py

class MyUserManager(BaseUserManager):
def create_user(self, username, email, password=None):

if not email:
raise ValueError('Users must have an email address')
if not username:
raise ValueError('Users must have an user name')

user = self.model(
email=self.normalize_email(email),
username = username
)

user.set_password(password)
user.save(using=self._db)
return user

def create_superuser(self, username, email, password):

user = self.create_user(
username,
password=password,
email = email,
)
user.is_admin = True
user.save(using=self._db)
return user


class MyUser(AbstractBaseUser):
id = models.AutoField(primary_key=True)

username = models.CharField(
verbose_name='user name',
max_length=30,
unique=True,
)

email = models.EmailField(
verbose_name='email address',
max_length=255,
unique=True,
)

is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)

sth_test = models.TextField(blank = True)

objects = MyUserManager()

USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email']

def __str__(self):
return self.username

def has_perm(self, perm, obj=None):
"Does the user have a specific permission?"
# Simplest possible answer: Yes, always
return True

def has_module_perms(self, app_label):
"Does the user have permissions to view the app `app_label`?"
# Simplest possible answer: Yes, always
return True

@property
def is_staff(self):
"Is the user a member of staff?"
# Simplest possible answer: All admins are staff
return self.is_admin

序列化器.py

class CreateUserSerializer(serializers.ModelSerializer):
class Meta:
model = get_user_model
fields = ('id', 'username', 'email', 'password', 'is_active')

email = serializers.EmailField(
required=True,
validators=[UniqueValidator(queryset=User.objects.all())]
)
username = serializers.CharField(
max_length=32,
validators=[UniqueValidator(queryset=User.objects.all())]
)
password = serializers.CharField(min_length=8, write_only=True)

def validate_email(self,value):
if User.objects.filter(email=value).exists():
raise serializers.ValidationError("err.")
return value

def create(self, validated_data):
user = User.objects.create_user(
validated_data['username'],
validated_data['email'],
validated_data['password'],
)
user.is_active = False
user.save()


message=render_to_string('accounts/account_activate_email.html',{
'user':user,
'domain':'localhost:8000/api/accounts/activate',
'uid':urlsafe_base64_encode(force_bytes(user.pk)).decode('utf-8'),
'token':account_activation_token.make_token(user)
})

mail_subject = 'Bplus'
to_email = user.email
AuthEmail = EmailMessage(mail_subject, message, to=[to_email])
AuthEmail.send()

return validated_data

views.py

class UserCreateAPI(generics.GenericAPIView):
serializer_class = CreateUserSerializer

def post(self, request, *args, **kwargs):
if len(request.data["username"]) < 4 or len(request.data["password"]) < 8:
body = {"message":"short field"}
return Response(body, status = 400)

serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
user = serializer.save()
user_for_auth = User.objects.get(username=user['username'])
login(request, user)

return Response(
{
"user":UserSerializer(
user, context=self.get_serializer_context()
).data,
"token":AuthToken.objects.create(user_for_auth),
}
)

我该如何修复这个错误?

enter image description here

最佳答案

您的序列化程序创建方法返回经过验证的数据而不是创建的对象。因为这是一个字典,所以这就是您最终传递给登录函数的内容。

您应该使用 return user 而不是 return validated_data

关于django 属性错误 : dict object has no attribute 'pk' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54013051/

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