<> 类型的对象不是 JSON 可序列化的 Django REST Framework

Object of type <> is not JSON serializable Django REST Framework

我正在尝试使用 Django Rest Framework 在基于 class 的视图中创建搜索功能,但它似乎不起作用。

当我尝试搜索一些客户时出现错误:

raise TypeError(f'Object of type {o.class.name} '
TypeError: Object of type Customer is not JSON serializable

我知道有一些问题看起来像我的问题,但我认为他们中的任何一个都不能帮助我。我对 Django REST 框架有点陌生。

我的看法:

class staff_search_partial(generics.ListAPIView):
    renderer_classes = [JSONRenderer, TemplateHTMLRenderer]
    template_name = 'BankApp/staff_search_partial.html'
    serializer_class = CustomerSerializer
    permissions_classes = [permissions.IsAuthenticated, ]

    def post(self, request):
        assert request.user.is_staff, 'Customer user routing staff view.'

        search_term = request.POST['search_term']
        print(type(search_term))
        customers = Customer.objects.filter(
                Q(user__username__contains=search_term)   |
                Q(user__firstname__contains=search_term) |
                Q(user__lastname__contains=search_term)  |
                Q(user__email__contains=search_term)      |
                Q(personalid__contains=search_term)      |
                Q(phone_contains=search_term)
            )[:15]

        return Response({'customers': customers})

客户模型:

class Customer(models.Model):
    user        = models.OneToOneField(User, primary_key=True, on_delete=models.PROTECT)
    rank        = models.ForeignKey(Rank, default=2, on_delete=models.PROTECT)
    personal_id = models.IntegerField(db_index=True)
    phone       = models.CharField(max_length=35, db_index=True)

    @property
    def full_name(self) -> str:
        return f'{self.user.first_name} {self.user.last_name}'

    @property
    def accounts(self) -> QuerySet:
        return Account.objects.filter(user=self.user)

    @property
    def can_make_loan(self) -> bool:
        return self.rank.value >= settings.CUSTOMER_RANK_LOAN

    @property
    def default_account(self) -> Account:
        return Account.objects.filter(user=self.user).first()

    def make_loan(self, amount, name):
        assert self.can_make_loan, 'User rank does not allow for making loans.'
        assert amount >= 0, 'Negative amount not allowed for loan.'
        loan = Account.objects.create(user=self.user, name=f'Loan: {name}')
        Ledger.transfer(
            amount,
            loan,
            f'Loan paid out to account {self.default_account}',
            self.default_account,
            f'Credit from loan {loan.pk}: {loan.name}',
            is_loan=True
        )

客户序列化程序:

class CustomerSerializer (serializers.ModelSerializer):

    class Meta:
        fields = ('user', 'rank', 'personal_id', 'phone', 'full_name', 'accounts', 'can_make_loan', 'default_account')
        model = Customer

您必须像这样编辑视图class

class staff_search_partial(generics.ListAPIView):
    renderer_classes = [JSONRenderer, TemplateHTMLRenderer]
    template_name = 'BankApp/staff_search_partial.html'
    serializer_class = CustomerSerializer
    permissions_classes = [permissions.IsAuthenticated, ]

    def post(self, request):
        assert request.user.is_staff, 'Customer user routing staff view.'

        search_term = request.POST['search_term']
        print(type(search_term))
        customers = Customer.objects.filter(
                Q(user__username__contains=search_term)   |
                Q(user__firstname__contains=search_term) |
                Q(user__lastname__contains=search_term)  |
                Q(user__email__contains=search_term)      |
                Q(personalid__contains=search_term)      |
                Q(phone_contains=search_term)
            )[:15]

        return customers

只是return客户不是字典

正如我在您的问题下的评论中所写,您应该序列化查询集

class staff_search_partial(generics.ListAPIView):
    renderer_classes = [JSONRenderer, TemplateHTMLRenderer]
    template_name = 'BankApp/staff_search_partial.html'
    serializer_class = CustomerSerializer
    permissions_classes = [permissions.IsAuthenticated, ]

    def post(self, request):
        assert request.user.is_staff, 'Customer user routing staff view.'

        search_term = request.POST['search_term']
        print(type(search_term))
        customers = Customer.objects.filter(
            Q(user__username__contains=search_term)   |
            Q(user__firstname__contains=search_term) |
            Q(user__lastname__contains=search_term)  |
            Q(user__email__contains=search_term)      |
            Q(personalid__contains=search_term)      |
            Q(phone_contains=search_term)
        )[:15]
        customers_data = CustomerSerializer(customers, many=True).data
        return Response({'customers': customers_data})

@TrueGopnik 回答正确。但我认为您不需要为搜索过滤器编写查询集。你可以使用 django restframework serachfilters。这让你的工作更少。请仔细阅读此文档 https://www.django-rest-framework.org/api-guide/filtering/#searchfilter