ValueError('字典更新序列元素#0的长度为9; 2是必需的',)不是JSON可序列化的

时间:2018-04-05 11:36:56

标签: python django dictionary django-rest-framework

我已经实现了下面的代码,并希望将dicts合并为一个。当我发出GET请求时,我收到以下日志。

ValueError('dictionary update sequence element #0 has length 9; 2 is required',) is not JSON serializable

如何将两个dicts合并为一个JSON?

class PayrollView(APIView):

    def get(self, request, pk, format=None):

        try:

            payslip = Payslip.objects.filter(
                id=pk).prefetch_related('user', 'employee').values(
                'payment_mode__name', 'payslip_no', 'allowance_types', 'deduction_types', 'month_ending', 'basic_salary__salary_value',
                'employee__user__last_name', 'employee__user__first_name',
                'employee__user__id'
            )

            result = payslip.aggregate(
                net_allowance=Sum('allowances__amount'))


            conv = dict(payslip, **result)

        except Exception as e:

            return Response(data=e, status=status.HTTP_400_BAD_REQUEST)
        return Response(data=conv, status=status.HTTP_200_OK)

模型

我有一个工资单模型,它也是Allowance模型中的ForeignKey

class Payslip(models.Model):
    employee = models.ForeignKey(Employee,
                                 related_name='payslip', null=True)
    basic_salary = models.ForeignKey(Salary)
    payment_mode = models.ForeignKey(PaymentMode,
                                     related_name='payslip', null=True)
    payslip_no = models.CharField(max_length=200, verbose_name="payslip no.")
    month_ending = models.DateField(blank=True, null=True)
    allowance_types = models.ManyToManyField(AllowanceType)
    deduction_types = models.ManyToManyField(DeductionType)
    created_at = models.DateTimeField(
        auto_now_add=True)
    modified_at = models.DateTimeField(
        auto_now=True)

    def __str__(self):
        return 'Payslip - %s - %s ' % (self.payslip_no, self.employee)

0 个答案:

没有答案