3

I have two models, Appointment and EmployeeEvent. I need to get data from these models and combine the result in to a single get api request.

urls.py

url(r'^calenderevents', calender_events)

views.py

@api_view(['GET'])
def calender_events(request):
    queryset1 = Appointment.objects.all()
    queryset2 = EmployeeEvent.objects.all()
    return Response({'Appointments':json.loads(serializers.serialize('json', queryset1)), 'EmployeeEvents': json.loads(serializers.serialize('json', queryset2))})

When I call the API, I am getting the result, but it includes some unwanted keys like "pk", "model", "fields" etc. Also in the appoinments result, I need the full customer object instead of the customer id. Is there any way to specify the CustomerSerializer along with the query set?

Results am getting

{
  "Appointments": [
    {
      "pk": "33a0fffb-326e-4566-bfb4-b146a87a4f3f",
      "model": "appointment.appointment",
      "fields": {
        "customer": "25503315-8bac-4070-87c1-86bf0630c846",
        "status": "Requested",
        "description": "Assigned appointment",
      }
    },
    {
      "pk": "9da806f5-77f1-41e6-a745-7be3f79d6f7a",
      "model": "appointment.appointment",
      "fields": {
        "customer": "25503315-8bac-4070-87c1-86bf0630c846",
        "status": "Requested",
        "description": "Assigned appointment",
      }
    }
  ],
  "EmployeeEvents": [
    {
      "pk": "f76b5de0-1ab8-4ac3-947d-15ba8941d97d",
      "model": "employee_event.employeeevent",
      "fields": {
        "event_name": "New Event",

        "start_date": "2017-02-17",
        "end_date": "2017-02-22"
      }
    },
    {
      "pk": "56f02290-370e-426c-951e-a93c57fde681",
      "model": "employee_event.employeeevent",
      "fields": {
        "event_name": "New Event",
        "start_date": "2017-02-02",
        "end_date": "2017-03-22"
      }
    }
  ]
}

Expected Result

{
  "Appointments": [
    {
      "id": "33a0fffb-326e-4566-bfb4-b146a87a4f3f",
      "customer": {
        "id": "25503315-8bac-4070-87c1-86bf0630c846",
        "firstname": "Customre 1",
        "photo_url": "imagepath",
      },
       "status": "Requested",
       "description": "Assigned appointment"
    },
    {
      "id": "9da806f5-77f1-41e6-a745-7be3f79d6f7a",
      "customer": {
        "id": "15ba8941d97d-8bac-4070-87c1-86bf0630c846",
        "firstname": "Customre 2",
        "photo_url": "imagepath",
      },
       "status": "Requested",
       "description": "Assigned appointment"
    },
    }
  ],
  "EmployeeEvents": [
    {
      "id": "f76b5de0-1ab8-4ac3-947d-15ba8941d97d",
      "event_name": "New Event 1",
      "start_date": "2017-02-17",
      "end_date": "2017-02-22"
    },
    {
      "id": "56f02290-370e-426c-951e-a93c57fde681",
      "event_name": "New Event 2”,
      "start_date": "2017-02-17",
      "end_date": "2017-02-22"
    }
  ]
}
2

1 Answer 1

16

You need to write a serializer to display the data in the desired format. Read the excellent tutorial to guide you though it properly. But if you want a quick hacky answer, then do something like this:

serializer = AppointmentSerializer(Appointment.objects.all(), many=True)
return Response(serializer.data)

Where the serializer looks something like this:

class AppointmentSerializer(serializers.ModelSerializer):
    customer = CustomerSerializer(required=False, allow_null=True)

    class Meta:
        model = Appointment
        fields = ('id', 'customer', 'status', 'etc...')
        related_object = 'customer'

class CustomerSerializer(serializers.ModelSerializer):
    class Meta:
        model = Customer
        fields = ('id', 'first_name', 'etc...')

Edit: updated to include example of a related object

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.