1

I managed to get data from the tables MemberDeclareRecept and Member with the following config files. Here I am looking for the MemberDeclareRecept.pk. But how can I get all the data if I search the Member.CoId instead? The MemberSearchByCode view gives all the members in the table but I can't get the specific member.

Here are my models

class Member(models.Model):
    Name = models.CharField(max_length=40,null=True)
    FirstName = models.CharField(max_length=40,null=True)
    DateBirth = models.DateField(,null=True)
    CoId = models.CharField(max_length=6,null=True)

class MemberDeclareRecept(models.Model):
    SyMember=models.ForeignKey(Member,verbose_name='Name member ',null=True,related_name='Member')
    DateDeclareRecept=models.DateField(('Date received',null=True)

And the serializers that are being used

class MemberDeclareSerializer(serializers.ModelSerializer):
    member = serializers.StringRelatedField(read_only=True)
    SyRecept=serializers.StringRelatedField(read_only=True)
    class Meta:
        model = MemberDeclareRecept
        fields=('id','member','SyRecept')

And the views that I am currently using

class MemberDeclareDetail(generics.ListCreateAPIView):
    queryset=MemberDeclareRecep.objects.all()
    serializer_class =MemberDeclareSerializer
    def get_object(self,pk):
        try:
            return self.queryset.get(pk=pk)
        except MemberDeclareRecep.DoesNotExist:
            raise Http404

    def get(self, request, pk,format=None):
        entries = self.get_object(pk)
        serializer = MemberDeclareSerializer(entries)
        return Response(serializer.data)

class MemberSearchByCode(generics.ListAPIView):
    serializer_class =MemberSerializer
    def get_queryset(self):
        member=self.request.QUERY_PARAMS.get(Co_id',None)
        if membre is not None:
            queryset = queryset.filter(member__Name=member
        return queryset
4
  • Missing some extra info: the version of DRF. there are several ways to implement filters in this list view. see django-rest-framework.org/api-guide/filtering/#filtering Commented Jan 15, 2015 at 22:56
  • Hi Michel. I user REST 3.0 Commented Jan 16, 2015 at 8:42
  • I've found this to solve my problem : class MemberSearch(generics.ListAPIView): serializer_class=MemberDeclareSerializer def get_queryset(self): member=self.kwargs['Co_id'] return member_declare_recept.objects.filter(member__Co_id=member) Any comments or suggestions? Commented Jan 17, 2015 at 12:36
  • That will do indeed! Good you solved it Commented Jan 17, 2015 at 12:37

1 Answer 1

1

It appears as though you've found an answer, based on the comment, and it's included below.

class MemberSearch(generics.ListAPIView):
    serializer_class=MemberDeclareSerializer

    def get_queryset(self):
        member = self.kwargs['Co_id']

        return member_declare_recept.objects.filter(member__Co_id=member)

It is important to note that this is not filtering the queryset based on query parameters, this is filtering it based on parameters present in the url. If you were filtering it based on query parameters, which is useful if you will need to get a list of all objects at once, the url that you would be using would be like

/api/members/?company=[co_id]

Which would make the company id optional. In your case though, the id is being embedded within the url itself. This is typically referred to as hierarchal urls, and it's generally not recommended, but your urls end up like

/api/company/[co_id]/members

Which is preferable for some, and even required in certain cases.


Now, if you wanted to use the query parameter instead of the url parameter for filtering, only a slight change would be required in your code.

class MemberSearch(generics.ListAPIView):
    serializer_class=MemberDeclareSerializer

    def get_queryset(self):
        company_id = self.request.query_parameters.get('company', None)

        if not company_id:
            return member_declare_recept.objects.all()

        return member_declare_recept.objects.filter(member__Co_id=company_id)

This has the additional advantage of also being support directly through django-filter and the DjangoFilterBackend.

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

1 Comment

Thanks Kevin for the formatting, grammar and recommendations ! This article is better now for sharing.

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.