Django Rest Framework AttributeError 'CustomUser' 对象没有属性 'user'



我有不同类型的用户。这就是为什么,每个用户类型都应该有自己的配置文件详细信息。因此,我正在尝试为这些概要文件编写API代码。但是,我无法解决此错误。我正在学习一门课程中的一个例子。我的代码与本例完全相似。我不明白我哪里错了。

我似乎在调和相关模型方面犯了一个错误。我检查了别名,并反复尝试解决它。我尝试了许多别名来解决它,但每次都不可能成功。

错误消息

AttributeError at /api/account/child-profile-update
Got AttributeError when attempting to get a value for field `user` on serializer `ChildProfileSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the `CustomUser` instance.
Original exception text was: 'CustomUser' object has no attribute 'user'.

自定义用户模型

class CustomUser(AbstractUser):
GENDER_CHOICES = (
(1, AccountStrings.CustomUserStrings.gender_choice_male),
(2, AccountStrings.CustomUserStrings.gender_choice_female),
(3, AccountStrings.CustomUserStrings.gender_choice_other),
)
USER_TYPE_CHOICES = (
(1, AccountStrings.CustomUserStrings.user_type_choice_default),
(2, AccountStrings.CustomUserStrings.user_type_choice_child),
(3, AccountStrings.CustomUserStrings.user_type_choice_parent),
(4, AccountStrings.CustomUserStrings.user_type_choice_instructor),
)
objects = CustomUserManager()
email = models.EmailField(max_length=255,
unique=True,
null=False,
blank=False)
identity_number = models.CharField(
max_length=11,
unique=True,
verbose_name=AccountStrings.CustomUserStrings.
identity_number_verbose_name)
birth_date = models.DateField(
null=True,
blank=True,
verbose_name=AccountStrings.CustomUserStrings.birth_date_verbose_name)
gender = models.PositiveSmallIntegerField(
choices=GENDER_CHOICES,
default=1,
verbose_name=AccountStrings.CustomUserStrings.gender_verbose_name)
user_type = models.PositiveSmallIntegerField(
choices=USER_TYPE_CHOICES,
default=1,
verbose_name=AccountStrings.CustomUserStrings.user_type_verbose_name)

子配置文件模型

class ChildProfile(models.Model):
user = models.OneToOneField(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
primary_key=True,
verbose_name=AccountStrings.ChildProfileStrings.user_verbose_name,
related_name="user_child")
city = models.ForeignKey(
City,
on_delete=models.SET_NULL,
null=True,
blank=True,
verbose_name=AccountStrings.ChildProfileStrings.city_verbose_name,
related_name="city_child_profiles")
hobbies = models.CharField(
max_length=500,
null=True,
blank=True,
verbose_name=AccountStrings.ChildProfileStrings.hobbies_verbose_name)

子配置文件序列化程序

from rest_framework.serializers import ModelSerializer
from account.models import ChildProfile
from django.contrib.auth import get_user_model
User = get_user_model()

class ChildProfileSerializer(ModelSerializer):
class Meta:
model = ChildProfile
fields = "__all__"
class UserChildSerializer(ModelSerializer):
childprofile = ChildProfileSerializer()
class Meta:
model = User
fields = ["id", "first_name", "last_name", "email", "identity_number", "gender", "birth_date", "childprofile"]

视图

from django.contrib.auth import get_user_model
User = get_user_model()
class ChildProfileUpdateAPIView(RetrieveUpdateAPIView):
queryset = User.objects.all()
serializer_class = ChildProfileSerializer
def get_object(self):
queryset = self.get_queryset()
obj = User.objects.get(id = self.request.user.id)
return obj

问题是由ChildProfileUpdateAPIView.get_object返回User/CustomUser实例引起的,而该实例很可能是ChildProfile实例。

从本质上讲,您正在使用User/CustomUser来对抗ChildProfileSerializer,这将失败并出现您看到的错误。

要解决此问题,请尝试:

def get_object(self):
obj = ChildProfile.objects.get(user=self.request.user)
return obj

最新更新