I'm relatively new to DRF and currently stuck at an issue. After lot of trials I tried looking around but couldn't find something what I want. In case this question has already been answered please do direct me to the original question. Thanks in advance for the help!
Problem:
I've a profile model that has one to one relation with User model. When I create a new user object the profile object is auto created as expected. I would like to create an API such that when I execute it like api/profile/ in addition to returning the profile model fields it should also return all the fields from User model i.e. first_name, last_name, username, email etc...
class Profile(models.Model):
rater = models.OneToOneField(User, on_delete=models.CASCADE, related_name='approval')
certified = models.ManyToManyField(Protocol, blank=True, default=None, related_name='trained')
is_approved = models.BooleanField(default=False)
My ProfileSerializer is as follows:
class ProfileSerializer(serializers.ModelSerializer):
approval = UserSerializer(read_only=True)
class Meta:
model = Profile
fields = '__all__'
When I run the API I get the following response:
[
{
"id": 1,
"rater": 3,
"certified": [],
"is_approved": false
}
]
How can I get the following response:
[
{
"id": 1,
"rater": 3,
"certified": [],
"is_approved": false,
"first_name": xyz,
"last_name": abc,
"username": 123,
"email": abc@gmail.com,
}
]