1

I have three classes representing different sections in my models: SectionA, SectionB, SectionC. Each of these sections have associated a set of items (class Item in my model).

I would like to get a json similar to this:

{
"sectionA": [
        {
            "id": 1, 
            "picture": "car_pic1", 
            "category": "cat1"
        }, 
        {
            "id": 3, 
            "picture": "car_pic1", 
            "category": "cat2"
        }, 
        {
            "id": 5, 
            "picture": "car_pic1", 
            "category": "cat3"
        }
],
"sectionB": [
        {
            "id": 2, 
            "picture": "car_pic1", 
            "category": "cat8"
        }, 
        {
            "id": 4, 
            "picture": "car_pic1", 
            "category": "cat9"
        }, 
        {
            "id": 7, 
            "picture": "car_pic1", 
            "category": "cat10"
        }, 
],
 "sectionC": [
            {
                "id": 9, 
                "picture": "car_pic1", 
                "category": "cat9"
            }, 
            {
                "id": 10, 
                "picture": "car_pic1", 
                "category": "cat9"
            }, 
            {
                "id": 11, 
                "picture": "car_pic1", 
                "category": "cat10"
            }, 
]
}

This json displays any three items associated to each section.

I would like to know how can I implement this using rest-framework. Basically I need to perform a query retrieving the three items for each section (since this json is not associated to a model object) and serialize all this into the json. I'm not sure where or how to perform these queries and I didn't have any success so far.

1 Answer 1

3

Finally I did it slightly different. My view just creates a dictionary with each section and its associated items:

class SectionList(APIView):
    """
    List three objects for each section.
    """
    def generate_data(self):
        #query to get the items of each section

        list_items = []
        list_items.append({"section" : "sectionA", "items" : secA_items})
        list_items.append({"section" : "sectionB", "items" : secB_items})
        list_items.append({"section" : "sectionC", "items" : secC_items})

        return list_items;

    def get(self, request, format=None):
        section_list = self.generate_data()
        serializer = SectionSerializer(section_list)
        return Response(serializer.data)

And this is the serializer I used:

class SectionSerializer(serializers.Serializer):
    section = serializers.CharField(max_length=200)
    items = ItemSerializer(many=True)
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.