0

I am trying to export data from two models for a user in the format of

"Level":[
      {
         "level":1,
         "book_ID":"96F7EBBD-4523-4473-978B-1BFD2F62926A",
         "date_added":"2020-03-06 21:58:42 +0900"
      },
      {
         "level":1,
         "book_ID":"76E8F992-7A7F-477D-B178-67BA5295DA6E",
         "date_added":"2020-03-06 21:59:12 +0900"
      },
]

"Key":[
      {
         "key":1,
         "book_ID":"96F7EBBD-4523-4473-978B-1BFD2F62926A",
         "date_added":"2020-03-06 21:58:42 +0900"
      },
      {
         "level":1,
         "book_ID":"76E8F992-7A7F-477D-B178-67BA5295DA6E",
         "date_added":"2020-03-06 21:59:12 +0900"
      },
]

In order to do this my code is

serializer.py

class LevelSerializer(serializers.ModelSerializer):

    book_ID = serializers.SlugRelatedField(
        source='book',
        many=False,
        read_only=True,
        slug_field='book_ID'
     )

    class Meta:
        model = Level
        fields = ["date_added",
                  'book_ID',
                  "level",
                  ]

class KeySerializer(serializers.ModelSerializer):

    book_ID = serializers.SlugRelatedField(
        source='book',
        many=False,
        read_only=True,
        slug_field='book_ID'
     )

    class Meta:
        model = Key
        fields = ["date_added",
                  'book_ID',
                  "mykey",
                  ]

view.py

def api_get_all(request):
    if request.method == 'GET':
        all_levels = Level.objects.filter(user=1)
        all_key  = Key.objects.filter(user=1)

        level_serializer = LevelSerializer(all_levels, many=True)
        key_serializer = KeySerializer(all_key, many=True)
        combine_serializer = level_serializer.data + key_serializer.data

        return Response(combine_serializer)

However if I execute my above implementation I get

[
      {
         "level":1,
         "book_ID":"96F7EBBD-4523-4473-978B-1BFD2F62926A",
         "date_added":"2020-03-06 21:58:42 +0900"
      },
      {
         "level":1,
         "book_ID":"76E8F992-7A7F-477D-B178-67BA5295DA6E",
         "date_added":"2020-03-06 21:59:12 +0900"
      },
      {
         "level":1,
         "book_ID":"76E8F992-7A7F-477D-B178-67BA5295DA6E",
         "date_added":"2020-03-06 21:59:12 +0900"
      },
      {
         "key":1,
         "book_ID":"96F7EBBD-4523-4473-978B-1BFD2F62926A",
         "date_added":"2020-03-06 21:58:42 +0900"
      }
]

How can I make it so that it is serialized in a parent key? Any help is appreciated

1 Answer 1

2
def api_get_all(request):
    ...

    level_serializer = LevelSerializer(all_levels, many=True)
    key_serializer = KeySerializer(all_key, many=True)

    return Response({
        "Level": level_serializer.data,
        "Key": key_serializer.data
    })
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.