2016-11-21 35 views
2

我在Python和Django的相对较新的多个值,Django的类型错误:get()方法得到了关键字参数 'INVOICE_ID'

我有以下REST API观点,

class InvoiceDownloadApiView(RetrieveAPIView): 
    """ 
    This API view will retrieve and send Terms and Condition file for download 
    """ 

    permission_classes = (IsAuthenticated,) 

    def get(self, invoice_id, *args, **kwargs): 

     if self.request.user.is_authenticated(): 
      try: 
       invoice = InvoiceService(user=self.request.user, organization=self.request.organization).invoice_download(
       invoice_id=invoice_id) 
      except ObjectDoesNotExist as e: 
       return Response(e.message, status=status.HTTP_404_NOT_FOUND) 

      if invoice: 
       response = HttpResponse(
       invoice, content_type='application/pdf') 
       response['Content-Disposition'] = 'inline; filename={0}'.format(
       invoice.name.split('/')[-1]) 
       response['X-Sendfile'] = smart_str(invoice) 
       return response 
      else: 
       return Response({"data": "Empty File"}, status=status.HTTP_400_BAD_REQUEST) 

使用以下网址,

urlpatterns = [ 
    url(r'^invoice/(?P<invoice_id>[0-9]+)/download/$', views.InvoiceDownloadApiView.as_view()), 

]

根URL模式是如下,

url(r'^api/payments/', include('payments.rest_api.urls', namespace="payments")), 

当我打电话终点,

localhost:8000/api/payments/invoice/2/download/ 

它出现以下错误,

TypeError at /api/payments/invoice/2/download/ 
get() got multiple values for keyword argument 'invoice_id' 

无法弄清楚什么是真正造成这个错误

+0

wi那个发票ID,我的数据库中只有一张发票。 – RTan

+0

请添加'invoice_download'方法的实现,所以我们可以帮助你。 – AKS

+0

@AKS不,这并不意味着。这将是一个MultipleObjectsReturned错误。 –

回答

8

第一参数(在self之后)查看方法始终为request。按照您定义它的方式,请求被传入为invoice_id方法,并且实际的invoice_id作为额外的kwarg被传入,因此是错误。

定义你的方法是这样的:

def get(self, request, invoice_id, *args, **kwargs): 
0

你也将得到错误,如果你自定义你的方法,如:

def get(request, token): 

def post(request, token): 

就像我做过一次......

相关问题