Я пытаюсь создать профиль пользователя с помощью Django Rest Framework, но постоянно получаю
Got AttributeError when attempting to get a value for field `user` on serializer `ProfileSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the `MyUser` instance.
Original exception text was: 'MyUser' object has no attribute 'user'.
Связано ли это с отношениями один-к-одному и тем, что моя модель пользователя определяется по электронной почте? Есть ли способ помочь ему?
Вот мои models.py :
class MyUser(AbstractBaseUser):
username = models.CharField(
max_length=300,
unique=True
)
email = models.EmailField(
max_length=255,
unique=True,
verbose_name='email address'
)
is_admin = models.BooleanField(default=False)
is_staff = models.BooleanField(default=False)
is_student = models.BooleanField(default=False)
is_teacher = models.BooleanField(default=False)
objects = UserManager()
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email']
def __str__(self):
return self.email
def get_short_name(self):
return self.email
class Profile (models.Model):
user = models.OneToOneField(MyUser, on_delete=models.CASCADE)
image = models.ImageField(default='default.jpg', upload_to='profile_pictures') #blank - true
bio = models.TextField(max_length=500, blank=True)
def __str__(self):
return f'{self.user.username} Profile'
serializers.py
class ProfileSerializer (serializers.ModelSerializer):
user = serializers.CharField(source='user.username')
image = serializers.ImageField(use_url=True)
class Meta:
model = Profile
fields = ('user', 'bio', 'image')
и views.py :
class ProfileView(APIView):
permission_classes = (permissions.IsAuthenticated,)
def get(self, request):
user = request.user
serializer = ProfileSerializer(user)
return Response(serializer.data, status=status.HTTP_200_OK)