and Postman returns
But the data should be:
# models.py
class UserProfile(models.Model):
"""
Extends Base User via 1-1 for profile information
"""
# Relations
user = models.OneToOneField(User, on_delete=models.CASCADE)
company = models.ForeignKey(Company, on_delete=models.CASCADE, null=True)
# Roles
is_ta = models.BooleanField(default=False) # indicates if user is part of TA department
def __str__(self):
return F'{self.user.first_name} {self.user.last_name}'
@receiver(post_save, sender=User)
def update_user_profile(sender, instance, created, **kwargs):
if created:
UserProfile.objects.create(user=instance)
instance.userprofile.save()
# serializers.py
class UserProfileSerializer(serializers.ModelSerializer):
class Meta:
model = UserProfile
fields = '__all__'
CodePudding user response:
You are incorrectly fetching the UserProfile
instance, instead of:
user_profile = UserProfile(user=request.user)
try:
user_profile = UserProfile.objects.get(user=request.user)
UPD:
In order to create a nested representation either set depth
attribute in Meta
of your serializer:
class UserProfileSerializer(serializers.ModelSerializer):
class Meta:
model = UserProfile
fields = '__all__'
depth = 1
or create a separate serializer for your related fields, e.g.:
class CompanySerializer(serializers.ModelSerializer):
class Meta:
model = Company
fields = your_fields
and set the serializer as field:
class UserProfileSerializer(serializers.ModelSerializer):
company = CompanySerializer()
class Meta:
model = UserProfile
fields = '__all__'