对象类型' ListSerializer'不是JSON可序列化的

时间:2017-11-09 17:22:29

标签: python django-rest-framework

我想获得所有客户数据和回复以及备注。 这是model.py

class Customer(models.Model):

name = models.CharField(max_length=200)
email_address = models.CharField(max_length=200)
phone_number = models.CharField(max_length=20)
age = models.SmallIntegerField(default=14)
remarks = models.ManyToManyField(Remark,null=True,blank=True)
created_at = models.DateTimeField(auto_now_add=True)

def __str__(self):
    return str(self.id)


class Response(models.Model):

question = models.ForeignKey(Question)
customer = models.ForeignKey(Customer)
response_text = models.CharField(max_length=100, null=True, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
uuid = models.UUIDField()

def __str__(self):
    return str(self.id)

这是serializers.py

class ResponseSerializer(ModelSerializer):
  class Meta:
    model = Response
    fields = '__all__'

class RemarksSerializer(ModelSerializer):
  class Meta:
    model = Remark
    fields = '__all__'

class CustomerInformationSerializer(ModelSerializer):
  remarks = RemarksSerializer(many=True)
  responses = serializers.SerializerMethodField()

  def get_responses(self, obj):
    responses = Response.objects.filter(customer=obj)
    return ResponseSerializer(responses, many=True)

  class Meta:
    model = Customer
    fields = ('name', 'email_address', 'phone_number', 'age', 'remarks', 'responses')

这是services.py

def customer_information(company_id=1):
  cus = Customer.objects.filter(remarks__company_id=company_id)
  return CustomerInformationSerializer(cus, many=True).data

这是views.py

class CustomerInformationView(APIView):
  def get(self, request):
     company_id = request.GET.get('company_id', 1)
     resp = {'data': customer_information(company_id)}
     return Response(data=resp, status=status.HTTP_200_OK)

这是url.py

 url(r'^customer/$', CustomerInformationView.as_view()),

我有这个问题。我该怎么解决这个问题。请指导我。

3 个答案:

答案 0 :(得分:0)

SIDE NOTE

首先,让我指出一个资源,我认为对于处理Django REST Framework的任何事情都很棒:

Classy Django REST Framework。这是一个很棒的资源,因为您可以轻松地深入了解源代码,看看您可能需要或不需要覆盖默认操作。

MY ANSWER

我建议您使用ListAPIView而不是使用APIView

它看起来像这样:

from rest_framework.generics import ListAPIView


class Customer(models.Model):
    name = models.CharField(max_length=200)
    email_address = models.CharField(max_length=200)
    phone_number = models.CharField(max_length=20)
    age = models.SmallIntegerField(default=14)
    remarks = models.ManyToManyField(Remark,null=True,blank=True)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return str(self.id)


class Response(models.Model):
    question = models.ForeignKey(Question)
    customer = models.ForeignKey(Customer, related_name='responses')
    response_text = models.CharField(max_length=100, null=True, blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    uuid = models.UUIDField()

    def __str__(self):
        return str(self.id)


class ResponseSerializer(ModelSerializer):
    class Meta:
        model = Response
        fields = '__all__'


class RemarksSerializer(ModelSerializer):
    class Meta:
        model = Remark
        fields = '__all__'


class CustomerInformationSerializer(ModelSerializer):
    remarks = RemarksSerializer(many=True)
    responses = ResponseSerializer(many=True)

    class Meta:
        model = Customer
        fields = ('name', 'email_address', 'phone_number', 'age', 'remarks', 'responses')


class CustomerInformationView(ListAPIView):
    queryset = Customer.objects.all()
    serializer_class = CustomerInformationSerializer
    lookup_field = 'remarks__company'

请注意我将related_name添加到customer模型的Response字段时所做的更改。请参阅more informationrelated_name的Django文档。简而言之,它会在responses模型上添加Customer作为字段名称,以便您可以通过该关系向后旅行。

这未经过测试,但这应该是一种更好的策略,可以在不需要get_responses方法或services.py的情况下执行您想要的操作。

答案 1 :(得分:0)

有些错误可能是因为路径末尾缺少“ /”,例如“ event-api” =不正确和“ event-api /”正确。那对我有用。希望你也有同样的问题。 错误的:path('event-api',views.event_view,name =“ event-view”) 正确:path('event-api /',views.event_view,name =“ event-view”)

答案 2 :(得分:0)

您视图中的

get函数应该返回具有响应的ins。