1

I am using Django as server side and angular as client side.

I want to fetch data from my django rest api backend. I saw a lot of tutorials about fetching data from an already existent modules. But what if I want to retrieve data that is a combination of several modules?

For example, I have two modules Reservations and Clubs. I want to retrieve json object that contains data from both of these modules, for specific club id.

Modules -

class Club(models.Model):
    name = models.CharField(max_length=1024)
    image_path = models.CharField(max_length=1024)

class Reservation(models.Model):
    club = models.ForeignKey('Club')
    user = models.CharField(max_length=1024)
    is_paid = models.BooleanField(default=False)

Serializers -

class ReservationSerializer(serializers.ModelSerializer):
    class Meta:
        model = Reservation
        fields  = ('club', 'user')

class ClubSerializer(serializers.ModelSerializer):
    class Meta:
        model = Club
        fields = ('id', 'name', 'surfaces')

View sets -

class ReservationViewSet(generics.ListAPIView):
    serializer_class = ReservationSerializer
    queryset = Reservation.objects.all()
    filter_backends = (filters.DjangoFilterBackend,)
    filter_fields = ('id', 'club')

class ClubViewSet(generics.ListAPIView):
    queryset = Club.objects.all()
    serializer_class = ClubSerializer
    filter_backends = (filters.DjangoFilterBackend,)
    filter_fields = ('id', 'name')

So, for this exmaple, I want that when I GET this url -
http://127.0.0.1:8000/api/initial_data?club=2
it will run a ViewSet that does some logic and then return a json of this format -

{club_id: 2, reservations: {1:"John", 2:"Doe", 3:"Bob"} }

And more generally speaking - How can I return my own custom json containing data from multiple modules, with given URL parameters (back to client side)?

EDIT - If I want to return a simple JSON, how should I do it with django DRF, considering the fact that each viewset is being mapped into a model/serializer?
Maybe using a simple JsonResponse..?

2 Answers 2

1

You can define one serializer as a field in another

class ReservationSerializer(serializers.ModelSerializer):
    ...

class ClubSerializer(serializers.ModelSerializer):
    reservations = ReservationSerializer(many=True, read_only=True)
    ...

Now ClubSerializer will return Reservations inside each Club

Sign up to request clarification or add additional context in comments.

6 Comments

@Serjik thanks for the quick reply. But I want to return a way more complex json from that one, that was just an example. I want to return data combined from multiple modlues
@John nop, enjoy to help
John: @Serjik answer is correct and points you in the right direction. If There's nothing that prevents you to return data models from other modules. It's in fact a common use case when you have a FK to User.
@Linovia Please refer my post edit. I want to return a json that contains many data types and values, not necessary Club and Reservations objects. Thanks again for your responses!
|
0

You can use APIView and override get method.

def get(self, request)

club = request.query_params.get('club', None)

"""
Do anything, access data, prepare dictionary or list with result
"""

return Response(result_dictionary_or_list)

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.