我已经实现了下面的代码,并希望将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)