I am quite new in using the REST Framework and I did not find in the tutorial how to achieve what I am looking for;
In my serialiser.py I have a class that is used to serialized MyUser models
class MyUserSerializer(ModelSerializer):
class Meta:
model = MyUser
fields = (
'email',
'first_name',
'last_name'
)
and in my view I am serializing a query that retrieve the user member in a team:
class EmployeeChartData(APIView):
#import pdb; pdb.set_trace()
authentication_classes = []
permission_classes = []
serializer_class = MyUserSerializer
def get_serializer_class(self):
return self.serializer_class
def get(self, request, format=None, *args, **kwargs):
team_name_list2 = Project.objects.get(id=kwargs['pk1']).team_id.members.all()
serializer=self.serializer_class
team_name_data = serializer(team_name_list2, many=True)
team_name_data = team_name_data.data
data = {
"team_name_list2":team_name_data,
}
Which give me an output for example:
"team_name_list2": [
{
"email": "[email protected]",
"first_name": "John",
"last_name": "Doe"
},
My question is how can I add to that dict custom data and data from other models that are linked to MyUser Model.
For example I have a Team Model
class Team(models.Model):
team_name = models.CharField(max_length=100, default = '')
team_hr_admin = models.ForeignKey(MyUser, blank=True, null=True)
members = models.ManyToManyField(MyUser, related_name="members")
How can I add all the team that the specific user is linked to ?
Thx you very much