我正在研究基于函数和类的视图,我发现基于类的视图非常有效。我有以下代码片段,并希望将基于函数的视图转换为基于类的视图。我该如何正确实现?
@api_view(['GET'])
def get_payslip_detail(request, pk, format=None):
try:
payslip = list(Payslip.objects.filter(id=pk).values(
'payment_mode','payslip_no','month_ending','basic','id','employee'
))[0]
# accesss object from list above
# access from object get id which is an integer >>>>> id=payslip['employee']
employee = list(Employee.objects.filter(id=payslip['employee']).values(
'user','hr_number','basic','tax_id_number', 'department', 'designation','id'))[0]
payslip['emp'] = employee
bank_acc = list(BankAccount.objects.filter(account_owner=payslip['employee']).values(
'bank_acc_number','bank_id'
))[0]
# append extra key to emp
payslip['acc_no']= bank_acc['bank_acc_number']
bank_name= list(Bank.objects.filter(id=bank_acc['bank_id']).values('name'))[0]
# print(bank_name)
payslip['bank_name'] = bank_name['name']
allowances = Allowance.objects.filter(payslip=pk).values('amount','name')
deductions = Deduction.objects.filter(payslip=pk).values('amount','name')
payslip['allowances'] = list(allowances)
payslip['deductions'] = list(deductions)
payslip['total_allowances'] = allowances.aggregate(net_allowance=Sum('amount'))['net_allowance']
payslip['total_deductions'] = deductions.aggregate(net_deduction=Sum('amount'))['net_deduction']
payslip['netpay'] = (payslip['total_allowances']+int(employee['basic']))-payslip['total_deductions']
except Exception as e:
# print(e)
return Response(data=e, status=status.HTTP_400_BAD_REQUEST)
return Response(data=payslip, status=status.HTTP_200_OK)
答案 0 :(得分:0)
您应该遵循有关基于课程的观点here的官方文档。
from rest_framework.views import APIView
class GetPayslipDetail(APIView):
def get(request, pk, format=None, *args, **kwargs):
try:
payslip = list(Payslip.objects.filter(id=pk).values(
'payment_mode', 'payslip_no', 'month_ending', 'basic', 'id', 'employee'
))[0]
# accesss object from list above
# access from object get id which is an integer >>>>> id=payslip['employee']
employee = list(Employee.objects.filter(id=payslip['employee']).values(
'user', 'hr_number', 'basic', 'tax_id_number', 'department', 'designation', 'id'))[0]
payslip['emp'] = employee
bank_acc = list(BankAccount.objects.filter(account_owner=payslip['employee']).values(
'bank_acc_number', 'bank_id'
))[0]
# append extra key to emp
payslip['acc_no'] = bank_acc['bank_acc_number']
bank_name = list(Bank.objects.filter(id=bank_acc['bank_id']).values('name'))[0]
# print(bank_name)
payslip['bank_name'] = bank_name['name']
allowances = Allowance.objects.filter(payslip=pk).values('amount', 'name')
deductions = Deduction.objects.filter(payslip=pk).values('amount', 'name')
payslip['allowances'] = list(allowances)
payslip['deductions'] = list(deductions)
payslip['total_allowances'] = allowances.aggregate(net_allowance=Sum('amount'))['net_allowance']
payslip['total_deductions'] = deductions.aggregate(net_deduction=Sum('amount'))['net_deduction']
payslip['netpay'] = (payslip['total_allowances'] + int(employee['basic'])) - payslip['total_deductions']
except Exception as e:
# print(e)
return Response(data=e, status=status.HTTP_400_BAD_REQUEST)
return Response(data=payslip, status=status.HTTP_200_OK)
在 urls.py 中更改为
from .views import GetPayslipDetail
urlpatterns = [
path('some-path/', GetPayslipDetail.as_view(), name='some_path'),
]