Answer the question
In order to leave comments, you need to log in
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"
}
}
}
},...........
class PointSerializer(serializers.ModelSerializer):
class Meta:
model = Point
fields = ["title", "description", "x", "y", "icon"]
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)
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question