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

webghufk  于 2023-01-14  发布在  Go
关注(0)|答案(3)|浏览(154)

我尝试使用Django Rest Framework在基于类的视图中创建一个搜索函数,但似乎不起作用。
当我试图搜索一些客户时,它给了我一个错误:

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
zaq34kh6

zaq34kh61#

正如我在您的问题下面的注解中所写的,您应该序列化queryset

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})
deyfvvtc

deyfvvtc2#

必须按如下方式编辑视图类

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

只是回头客不是字典

uurv41yg

uurv41yg3#

@TrueGopnika的答案是正确的,但我认为搜索过滤器不需要写queryset,可以使用Django REST框架searchfilters,这样可以减少工作量,请浏览此文档https://www.django-rest-framework.org/api-guide/filtering/#searchfilter

相关问题