1

我对 django-rest-framework 很陌生。我正在尝试将示例无限深度嵌套和超链接用于drf-nested-routers中的嵌套资源

我添加了一个 MailReply 对象只是为了尝试无限深度嵌套。当我尝试访问收件人时,它给了我这个 AttributeError。

AttributeError at /api/clients/1/maildrops/1/recipients/
'MailRecipient' object has no attribute 'maildrop'

序列化程序.py

class MailReplySerializer(NestedHyperlinkedModelSerializer):
    parent_lookup_kwargs = {
        'mailrecipient_pk' : 'mailrecipient__pk',
    }
    class Meta:
        model = MailReply
        fields = ('id','url','title',)
class MailRecipientMailReplySerializer(NestedHyperlinkedModelSerializer):
    parent_lookup_kwargs = {
        'mailrecipient_pk' : 'mailrecipient__pk',
    }
    class Meta:
        model = MailReply
        fields = ('url','title')  
class MailRecipientSerializer(NestedHyperlinkedModelSerializer):
    parent_lookup_kwargs = {
        'maildrop_pk' : 'maildrop__pk',
    }
    class Meta:
        model = MailRecipient
        fields = ('id','url','name','mailreplies')
    mailreplies = MailRecipientMailReplySerializer(many=True, read_only=True)
class MailDropMailRecipientSerializer(NestedHyperlinkedModelSerializer):
    parent_lookup_kwargs = {
        'maildrop_pk' : 'maildrop__pk'
    }
    class Meta:
        model = MailRecipient
        fields = ('url','name')   
class MailDropSerializer(NestedHyperlinkedModelSerializer):
    parent_lookup_kwargs = {
        'client_pk' : 'client__pk'
    }
    class Meta:
        model = MailDrop
        fields = ('id','url','title','recipients')
    recipients = MailDropMailRecipientSerializer(many=True, read_only=True)
class ClientMailDropSerializer(NestedHyperlinkedModelSerializer):
    parent_lookup_kwargs = {
        'client_pk' : 'client__pk',
    }
    class Meta:
        model = MailDrop
        fields = ('url','title',)    
class ClientSerializer(HyperlinkedModelSerializer):
    class Meta:
        model = Client
        fields = ('id','url','name','maildrops')
    maildrops =ClientMailDropSerializer(many=True, read_only=True)

视图.py

    class ClientViewSet(viewsets.ViewSet):
        serializer_class = ClientSerializer
        queryset = Client.objects.all()
        def list(self, request,):
            queryset = Client.objects.filter()
            serializer = ClientSerializer(queryset, many=True, context={'request': request})
            return Response(serializer.data)

        def retrieve(self, request, pk=None):
            queryset = Client.objects.filter()
            client = get_object_or_404(queryset, pk=pk)
            serializer = ClientSerializer(client, context={'request': request})
            return Response(serializer.data)

    class MailDropViewSet(viewsets.ViewSet):
        serializer_class = MailDropSerializer

        def list(self, request, client_pk=None):
            queryset = MailDrop.objects.filter(client=client_pk)
            serializer = MailDropSerializer(queryset, many=True, context={'request': request})
            return Response(serializer.data)

        def retrieve(self, request, pk=None, client_pk=None):
            queryset = MailDrop.objects.filter(pk=pk, client=client_pk)
            maildrop = get_object_or_404(queryset, pk=pk)
            serializer = MailDropSerializer(maildrop, context={'request': request})
            return Response(serializer.data)

    class MailRecipientViewSet(viewsets.ViewSet):
        serializer_class = MailRecipientSerializer

        def list(self, request, client_pk=None, maildrop_pk=None):
            queryset = MailRecipient.objects.filter(mail_drop__client=client_pk, mail_drop=maildrop_pk)
            serializer = MailRecipientSerializer(queryset, many=True, context={'request': request})
            return Response(serializer.data)

        def retrieve(self, request, pk=None, client_pk=None, maildrop_pk=None):
            queryset = MailRecipient.objects.filter(pk=pk, mail_drop=maildrop_pk, mail_drop__client=client_pk)
            maildrop = get_object_or_404(queryset, pk=pk)
            serializer = MailRecipientSerializer(maildrop, context={'request': request})
            return Response(serializer.data)

    class MailReplyViewSet(viewsets.ViewSet):
        serializer_class = MailReplySerializer

        def list(self, request, client_pk=None, maildrop_pk=None, mailrecipient_pk=None):
            queryset = MailReply.objects.filter(mail_recipient=mailrecipient_pk, mail_recipient__mail_drop=maildrop_pk, mail_recipient__mail_drop__client=client_pk)
            serializer = MailReplySerializer(queryset, many=True, context={'request': request})
            return Response(serializer.data)

        def retrieve(self, request, pk=None, client_pk=None, maildrop_pk=None, mailrecipient_pk=None):
            queryset = MailReply.objects.filter(pk=pk, mail_recipient=mailrecipient_pk, mail_recipient__mail_drop=maildrop_pk, mail_recipient__mail_drop__client=client_pk)
            mailreply = get_object_or_404(queryset, pk=pk)
            serializer = MailReplySerializer(mailreply, context={'request': request})
            return Response(serializer.data)

非常感谢任何建议或示例,在此先感谢。

4

1 回答 1

1

问题实际上出在我的模型而不是 maildrop 中,我使用 mail_drop 作为 MailRecipient 的属性。

这是工作代码。

序列化程序.py

class MailReplySerializer(NestedHyperlinkedModelSerializer):
    parent_lookup_kwargs = {
        'client_pk' : 'mail_recipient__mail_drop__client__pk', 'maildrop_pk' : 'mail_recipient__mail_drop__pk', 'mailrecipient_pk' : 'mail_recipient__pk'
    }
    class Meta:
        model = MailReply
        fields = ('id','url','title',)
class MailRecipientMailReplySerializer(NestedHyperlinkedModelSerializer):
    parent_lookup_kwargs = {
        'client_pk' : 'mail_recipient__mail_drop__client__pk', 'maildrop_pk' : 'mail_recipient__mail_drop__pk', 'mailrecipient_pk' : 'mail_recipient__pk'
    }
    class Meta:
        model = MailReply
        fields = ('url','title')  
class MailRecipientSerializer(NestedHyperlinkedModelSerializer):
    parent_lookup_kwargs = {
        'client_pk' : 'mail_drop__client__pk', 'maildrop_pk' : 'mail_drop__pk'
    }
    class Meta:
        model = MailRecipient
        fields = ('id','url','name','mailreplies')
    mailreplies = MailRecipientMailReplySerializer(many=True, read_only=True)
class MailDropMailRecipientSerializer(NestedHyperlinkedModelSerializer):
    parent_lookup_kwargs = {
        'client_pk' : 'mail_drop__client__pk', 'maildrop_pk' : 'mail_drop__pk'
    }
    class Meta:
        model = MailRecipient
        fields = ('url','name')   
class MailDropSerializer(NestedHyperlinkedModelSerializer):
    parent_lookup_kwargs = {
        'client_pk' : 'client__pk'
    }
    class Meta:
        model = MailDrop
        fields = ('id','url','title','recipients')
    recipients = MailDropMailRecipientSerializer(many=True, read_only=True)
class ClientMailDropSerializer(NestedHyperlinkedModelSerializer):
    parent_lookup_kwargs = {
        'client_pk' : 'client__pk',
    }
    class Meta:
        model = MailDrop
        fields = ('url','title',)    
class ClientSerializer(HyperlinkedModelSerializer):
    class Meta:
        model = Client
        fields = ('id','url','name','maildrops')
    maildrops =ClientMailDropSerializer(many=True, read_only=True)

视图.py

class ClientViewSet(viewsets.ViewSet):
    serializer_class = ClientSerializer
    queryset = Client.objects.all()
    def list(self, request,):
        queryset = Client.objects.filter()
        serializer = ClientSerializer(queryset, many=True, context={'request': request})
        return Response(serializer.data)

    def retrieve(self, request, pk=None):
        queryset = Client.objects.filter()
        client = get_object_or_404(queryset, pk=pk)
        serializer = ClientSerializer(client, context={'request': request})
        return Response(serializer.data)

class MailDropViewSet(viewsets.ViewSet):
    serializer_class = MailDropSerializer

    def list(self, request, client_pk=None):
        queryset = MailDrop.objects.filter(client=client_pk)
        serializer = MailDropSerializer(queryset, many=True, context={'request': request})
        return Response(serializer.data)

    def retrieve(self, request, pk=None, client_pk=None):
        queryset = MailDrop.objects.filter(pk=pk, client=client_pk)
        maildrop = get_object_or_404(queryset, pk=pk)
        serializer = MailDropSerializer(maildrop, context={'request': request})
        return Response(serializer.data)

class MailRecipientViewSet(viewsets.ViewSet):
    serializer_class = MailRecipientSerializer

    def list(self, request, client_pk=None, maildrop_pk=None):
        queryset = MailRecipient.objects.filter(mail_drop__client=client_pk, mail_drop=maildrop_pk)
        serializer = MailRecipientSerializer(queryset, many=True, context={'request': request})
        return Response(serializer.data)

    def retrieve(self, request, pk=None, client_pk=None, maildrop_pk=None):
       queryset = MailRecipient.objects.filter(pk=pk, mail_drop=maildrop_pk, mail_drop__client=client_pk)
       mailrecipient = get_object_or_404(queryset, pk=pk)
       serializer = MailRecipientSerializer(mailrecipient, context={'request': request})
       return Response(serializer.data)

class MailReplyViewSet(viewsets.ViewSet):
   serializer_class = MailReplySerializer

   def list(self, request, client_pk=None, maildrop_pk=None, mailrecipient_pk=None):
      queryset = MailReply.objects.filter(mail_recipient=mailrecipient_pk, mail_recipient__mail_drop=maildrop_pk, mail_recipient__mail_drop__client=client_pk)
      serializer = MailReplySerializer(queryset, many=True, context={'request': request})
      return Response(serializer.data)

   def retrieve(self, request, pk=None, client_pk=None, maildrop_pk=None, mailrecipient_pk=None):
      queryset = MailReply.objects.filter(pk=pk, mail_recipient=mailrecipient_pk, mail_recipient__mail_drop=maildrop_pk, mail_recipient__mail_drop__client=client_pk)
      mailreply = get_object_or_404(queryset, pk=pk)
      serializer = MailReplySerializer(mailreply, context={'request': request})
      return Response(serializer.data)
于 2020-03-14T08:45:20.433 回答