Omar Jandali
Omar Jandali

Reputation: 824

Trying to filter django view serializer based on nested model foreignkey

I have a django app. Within it, I have 2 models. One of them is a group and the other is a member model. Then member model has a foriegnkey which is the group model. I serialized the models and now I am trying to get the api to to work as I want. I want to be able to call an api that has a group name at the end that is passed as a filter for the members to only return the members of the group name. I have 2 urls. the first one returns all the members of every group while I want the second one to return just the members of a certain group. I have tried a few different things from suggestions but none of them are working. This is the last thing I tried below. I will add my code below.

Models:

class Group(models.Model):
    name = models.CharField(max_length=42)
    description = models.CharField(max_length=220)
    user_count = models.IntegerField()
    status = models.CharField(max_length=12)
    image = models.ImageField(upload_to='group_images/')
    created_by = models.ForeignKey(User, on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.name + ' - ' + self.created_by.username

class Member(models.Model):
    group = models.ForeignKey(Group, on_delete=models.CASCADE)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    host = models.BooleanField(default=False)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.group.name + ' - ' + self.user.username 

Urls:

path('members/', MemberListView.as_view()),
path('members/<name>', MemberGroupListView.as_view()),

views:

class MemberListView(ListAPIView):
    queryset = Member.objects.all()
    serializer_class = MemberSerializer

class MemberGroupListView(ListAPIView):
    queryset = Member.objects.all()
    serializer_class = MemberSerializer
    filter_backends = (filters.DjangoFilterBackend,)
    filterset_fields = ('user', 'host', 'group')

    def get_queryset(self):
        return self.queryset.filter(group__name=self.request.query_params.get('name'))

the MemberListView is working properly but the MemberGroupListView is not working.

enter image description here


Update:

added the serialzers:

class GroupSerializer(serializers.ModelSerializer):
    class Meta:
        model = Group
        fields = ('name', 'description', 'user_count', 'status', 'image', 'created_by')

class MemberSerializer(serializers.ModelSerializer):
    class Meta:
        model = Member
        fields = ('group', 'user', 'host')

Upvotes: 1

Views: 168

Answers (1)

pomo_mondreganto
pomo_mondreganto

Reputation: 2074

Url parameters, such as name in your case (path('members/<name>', MemberGroupListView.as_view()),) are stored in self.kwargs in class-based views, so your get_queryset should be:

def get_queryset(self):
    return self.queryset.filter(group__name=self.kwargs.get('name'))

Upvotes: 1

Related Questions