Python 字段';id';应为一个数字,但得到';礼品';
我正在使用Django/Rest处理一个小项目,我尝试在浏览器中打开名为gives的GenericViewSet方法,但如果添加参数,则无法工作 这是我的代码:Python 字段';id';应为一个数字,但得到';礼品';,python,django,Python,Django,我正在使用Django/Rest处理一个小项目,我尝试在浏览器中打开名为gives的GenericViewSet方法,但如果添加参数,则无法工作 这是我的代码: @action(methods=['get'], detail=False) def gifts(self, request, pk=None): getRelatedContact = Contact.objects.filter(account = pk).exclude(id = self.contactID) s
@action(methods=['get'], detail=False)
def gifts(self, request, pk=None):
getRelatedContact = Contact.objects.filter(account = pk).exclude(id = self.contactID)
serializer = ContactSerializer(getRelatedContact, many=True)
return Response(serializer.data)
这是我的URL.py
from django.urls import path
from apps.contacts.api.views import ContactView
from rest_framework import routers
router = routers.DefaultRouter()
router.register(r'contacts', ContactView, basename="contact")
urlpatterns = router.urls
以下网址:
/contacts/gifts/1/
但是如果我删除数字1,它可以正常工作,我的意思是如果我不传递参数,它可以正常工作
如何使用url中的参数访问我的gifts方法什么是
self.contactID
?如果要按pk
进行筛选,则需要设置detail=True
notFalse@IainShelvington删除了self.contactID,只需在调试时将其忘在代码中礼品视图的url在哪里?共享您的模型和完整的错误回溯。。