U
U
undeadter2019-04-19 21:13:07
Django
undeadter, 2019-04-19 21:13:07

How to serialize a django model as a list?

class HistoryView(ListAPIView):
    permission_classes = (IsAuthenticated,)
    serializer_class = serializers.private.HistorySerializer
    model = serializer_class.Meta.model

    def get_queryset(self):
        user = self.request.user
        queryset = self.model.objects.filter(Q(user_1=user) | Q(user_2=user))
        return queryset

class HistorySerializer(ModelSerializer):
    class Meta:
        model = models.exchange.History
        fields = [
            'date',
            'type',
            'name',
        ]

Returns:
[
    {
        "date": 1555690476.155725,
        "type": false,
        "name":  "name",
    },
    .......
    .......
]

How can I get the data as an array of arrays?
[
        [1555690476.155725, false, "name"],
        [...],
         ...
],

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
alternativshik, 2019-04-20
@undeadter

override the serializer method to_representation(self, obj) like this

def to_representation(self, obj):
        return [obj.id, obj.date, obj.name]

D
Dimonchik, 2019-04-20
@dimonchik2013

Can't you convert a dictionary to a list?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question