0

Hello currently I implemented django-treebeard package for archive tree structure for categories.

Here I faced problem in response with URL of image.

I can't see Absolute URL of image categories

NOTE - serializer is not working with this recursive tree structure. I already know that you can pass context = {'request':request} in serializer but in my case, it's not working because it is recursive tree structure.

views.py

class CategoryAPI(APIView):
    def get(self, request):
        try:
            if CategoryModel.objects.count() > 0:
                categories = CategoryModel.dump_bulk()
                # serializer = CategorySerializer(instance=categories, many=True)
                # print(serializer)
                return Response({"status":True,"categories":categories}, status=status.HTTP_200_OK)
            else:
                return Response({'message': 'Categories not found.'}, status=status.HTTP_200_OK)
        except Exception as e:
            return Response({'error':str(e)})

API GET Response is like this

{
    "status": true,
    "categories": [
        {
            "data": {
                "name": "Fruits",
                "image": "Categories/download_3_Vg6Rv37.jfif"
            },
            "id": 1,
            "children": [
                {
                    "data": {
                        "name": "Apple",
                        "image": "Categories/download_6zbqDvn.jfif"
                    },
                    "id": 2,
                    "children": [
                        {
                            "data": {
                                "name": "Green Kasmiri Apple",
                                "image": "Categories/download_2_HaMCXyc.jfif"
                            },
                            "id": 10
                        }
                    ]
                },
                {
                    "data": {
                        "name": "Banana",
                        "image": "Categories/download_4_Mn5h9nL.jfif"
                    },
                    "id": 3
                },
                {
                    "data": {
                        "name": "Mango",
                        "image": "Categories/download_5_4RkBFcS.jfif"
                    },
                    "id": 5
                },
                {
                    "data": {
                        "name": "Orange",
                        "image": "Categories/download_6_vWVEQgm.jfif"
                    },
                    "id": 4
                }
            ]
        },
        {
            "data": {
                "name": "Health",
                "image": "Categories/images_fwp1mBB.jfif"
            },
            "id": 6
        }
    ]
}

I want to Responce is like this

{
    "status": true,
    "categories": [
        {
            "data": {
                "name": "Fruits",
                "image": "http://127.0.0.1:8000/media/Categories/download_3_Vg6Rv37.jfif"
            },
            "id": 1,
            "children": [
                {
                    "data": {
                        "name": "Apple",
                        "image": "http://127.0.0.1:8000/media/Categories/download_6zbqDvn.jfif"
                    },
                    "id": 2,
                    "children": [
                        {
                            "data": {
                                "name": "Green Kasmiri Apple",
                                "image": "http://127.0.0.1:8000/media/Categories/download_2_HaMCXyc.jfif"
                            },
                            "id": 10
                        }
                    ]
                },
                {
                    "data": {
                        "name": "Banana",
                        "image": "http://127.0.0.1:8000/media/Categories/download_4_Mn5h9nL.jfif"
                    },
                    "id": 3
                },
                {
                    "data": {
                        "name": "Mango",
                        "image": "http://127.0.0.1:8000/media/Categories/download_5_4RkBFcS.jfif"
                    },
                    "id": 5
                },
                {
                    "data": {
                        "name": "Orange",
                        "image": "http://127.0.0.1:8000/media/Categories/download_6_vWVEQgm.jfif"
                    },
                    "id": 4
                }
            ]
        },
        {
            "data": {
                "name": "Health",
                "image": "http://127.0.0.1:8000/media/Categories/images_fwp1mBB.jfif"
            },
            "id": 6
        }
    ]
}
Donald Duck
  • 8,409
  • 22
  • 75
  • 99

0 Answers0