'Need to return a Single JSON response for two serializers in a customized way

serializers

class Consolidated_Final(serializers.ModelSerializer):
    users = serializers.SerializerMethodField()

    class Meta:
        model= Users
        fields= ['users','id','employee_name','billable_and_non_billable']

    def get_users(self,obj):
        query = Add_Job.objects.all()
        return Add_Job_Serializers(query, many =True).data

views

@api_view(['GET'])
def final_view(request):
    q1 = Users.objects.values('id','employee_name','billable_and_non_billable',)
    query = Consolidated_Final(q1,many=True)
    return Response(query.data)

It is returning me the JSON response as

[
    {
        "users": [
            {
                "id": 1,
                "job_name": "timesheet",
                "Hours": "12:12:00",
                "start_Date": "0121-12-12T12:12:00+05:53",
                "end_Date": "1211-12-12T12:12:00+05:53",
                "client": 1,
                "project": 1,
                "user": 1
            },
            {
                "id": 2,
                "job_name": "Add",
                "Hours": "12:12:00",
                "start_Date": "1212-12-12T12:01:00+05:53",
                "end_Date": "0121-12-12T12:12:00+05:53",
                "client": 1,
                "project": 2,
                "user": 2
            }
        ],
        "id": 1,
        "employee_name": "vinoth",
        "billable_and_non_billable": "Billable"
    },
    {
        "users": [
            {
                "id": 1,
                "job_name": "timesheet",
                "Hours": "12:12:00",
                "start_Date": "0121-12-12T12:12:00+05:53",
                "end_Date": "1211-12-12T12:12:00+05:53",
                "client": 1,
                "project": 1,
                "user": 1
            },
            {
                "id": 2,
                "job_name": "Add",
                "Hours": "12:12:00",
                "start_Date": "1212-12-12T12:01:00+05:53",
                "end_Date": "0121-12-12T12:12:00+05:53",
                "client": 1,
                "project": 2,
                "user": 2
            }
        ],
        "id": 2,
        "employee_name": "Maari",
        "billable_and_non_billable": "Billable"
    },
    {
        "users": [
            {
                "id": 1,
                "job_name": "timesheet",
                "Hours": "12:12:00",
                "start_Date": "0121-12-12T12:12:00+05:53",
                "end_Date": "1211-12-12T12:12:00+05:53",
                "client": 1,
                "project": 1,
                "user": 1
            },
            {
                "id": 2,
                "job_name": "Add",
                "Hours": "12:12:00",
                "start_Date": "1212-12-12T12:01:00+05:53",
                "end_Date": "0121-12-12T12:12:00+05:53",
                "client": 1,
                "project": 2,
                "user": 2
            }
        ],
        "id": 3,
        "employee_name": "Maari",
        "billable_and_non_billable": "Billable"
    }
]

I need the JSON response as

[
    {
        "users": [
            {
                "id": 1,
                "job_name": "timesheet",
                "Hours": "12:12:00",
                "start_Date": "0121-12-12T12:12:00+05:53",
                "end_Date": "1211-12-12T12:12:00+05:53",
                "client": 1,
                "project": 1,
                "user": 1
            }
        ],
        "id": 1,
        "employee_name": "vinoth",
        "billable_and_non_billable": "Billable"
    },
    {
        "users": [
            {
                "id": 2,
                "job_name": "Add",
                "Hours": "12:12:00",
                "start_Date": "1212-12-12T12:01:00+05:53",
                "end_Date": "0121-12-12T12:12:00+05:53",
                "client": 1,
                "project": 2,
                "user": 2
            }
        ],
        "id": 2,
        "employee_name": "Maari",
        "billable_and_non_billable": "Billable"
    },
    {
        "users": [
        ],
        "id": 3,
        "employee_name": "Maari",
        "billable_and_non_billable": "Billable"
    }
]

I need to get the single JSON response by combining for User id: 1 need to add the Job details user:1 as a single JSON response. But now it is displaying all the jobs under all the users. Kindly help me to resolve this issue.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source