0

I have an API that can list several buildings. Each building belongs to several building groups and each building group contains several buildings.

I want to show single fields of one building group. More specifically I want to show all buildings of one building group in my RetrieveAPIView.

I can list a single BuildingGroup instance using the generic view like so:

class BuildingGroupRetrieveAPIView(RetrieveAPIView):
    serializer_class = BuildingGroupSerializer
    queryset = BuildingGroup.buildings.all()

I assume that I can overwrite the get method to only display a single field of that retrieved object. Specifically I want to display all the objects that are in my many to many relation. Or better to say, I want to retrieve all the complete data within my m2m relation.

Here are my models:

class Building(models.Model):
    name  = models.CharField(max_length=120, null=True, blank=True)

    def __str__(self):
        return self.name


class BuildingGroup(models.Model):
    description           = models.CharField(max_length=500, null=True, blank=True)
    buildings             = models.ManyToManyField(Building, default=None, blank=True)

I tried this without success:

 def get(self):
        building_group = BuildingGroup.objects.get(id='id')
        qs = building_group.buildings.all()
        return qs

my serializer

class BuildingGroupSerializer(serializers.ModelSerializer):

    class Meta:

        model = BuildingGroup

        fields = (
            'description',
             .....
        )

I can attach a screenshot to be more clear.

Any help is highly appreciated. Thanks in advance

Here is my full view:


class BuildingGroupAPIView(ListAPIView):

    permission_classes          = [permissions.IsAdminUser]
    authentication_classes      = [SessionAuthentication]

    serializer_class = BuildingGroupSerializer
    passed_id = None

    def get_queryset(self):
        qs = BuildingGroup.objects.all()
        query = self.request.GET.get('q')
        if query is not None:
            qs = qs.filter(name=query)
        return qs


class BuildingGroupRetrieveAPIView(RetrieveAPIView):
    serializer_class = BuildingGroupSerializer
    queryset = BuildingGroup.buildings.all()

    def get(self):
        building_group = BuildingGroup.objects.get(id='id')
        qs = building_group.buildings.all()
        return qs

enter image description here

Micromegas
  • 1,499
  • 2
  • 20
  • 49
  • The **`RetrieveAPIView`** supposed to return ***only one instance***, in your case a single **`BuildingGroup`** instance, which is chosen by the id/pk provided in the URL. – JPG Jun 12 '19 at 11:20
  • I think I was posing my question wrong. I do get a single instance back from the RetrieveAPIView. Now what I get looks something like the screenshot I added. What I want now, is, instead of the array [1,2,3,4], display the full information of my many2many relation. Not only the id. That is to say the information of all my buildings within the building group. Sorry for the confusion. I just thought that maybe this needs to be done in the serializer? Or via overwriting the get_object method? I'm a bit lost – Micromegas Jun 12 '19 at 11:51
  • I added my serializer – Micromegas Jun 12 '19 at 11:55
  • found my answer here: https://stackoverflow.com/questions/33182092/django-rest-framework-serializing-many-to-many-field, I needed to change my serializer. Thanks JPG for your help! – Micromegas Jun 12 '19 at 12:13

0 Answers0