S
S
spkar2018-08-23 17:21:21
Django
spkar, 2018-08-23 17:21:21

Mapping data property to json (Django)?

Good afternoon! Tell me how to do it?
I need to get an array of linked list from 'data'.
I have 'links', 'data', 'meta' output in JSON.
If I use serializer.data , then I get the values ​​of the data element along with it.

{
    "data": [
        {
            "type": "Point",
            "id": "48",
            "attributes": {
                "title": "Бургерная",
                "description": "ходог",
                "x": "123",
                "y": "321"
            },
            "relationships": {
                "icon": {
                    "data": {
                        "type": "Icon",
                        "id": "5"
                    }
                }
            }
        },...........

serialize.py
class PointSerializer(serializers.ModelSerializer):
    class Meta:
        model = Point
        fields = ["title", "description", "x", "y", "icon"]

views.py
class PointViewSet(viewsets.ModelViewSet):
    queryset = Point.objects.all()
    serializer_class = PointSerializer
    permission_classes = [permissions.AllowAny]

    def list(self, request, *args, **kwargs):
        queryset = Point.objects.all()
        description = self.request.query_params.get('description', None)
        if description:
            queryset = queryset.filter(description__icontains=description)
        serializer = PointSerializer(queryset, many=True)
        print(serializer.data)
        return Response(serializer.data)

urls.py
router = routers.DefaultRouter()
router.register('icons', views.IconViewSet, base_name='iconRouter')
router.register('points', views.PointViewSet, base_name='pointRouter')

urlpatterns = [
                  path('api/', include(router.urls)),
                  url(r'^$', views.test),
              ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
KhD, 2018-08-24
@KhD

It's unclear!!!!!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question