Django Rest 框架 Copeapi LinkLookupError
Django Rest Framework Copeapi LinkLookupError
我在尝试获取 Django Rest Framework 提供的端点时收到 LinkLookupError
。此错误仅在单个成功请求后发生。我的 DRF view.py
看起来像:
class RecordViewSet(viewsets.ReadOnlyModelViewSet):
"""
API endpoint that allows Records to be viewed or edited.
"""
filter_backends = (SimpleRecordFilterBackend,)
serializer_class = RecordSerializer
permission_classes = (IsAuthenticated,)
@action(detail=False, url_path='tagsum/(?P<account>[^/.]+)/(?P<tag>[^/.]+)')
def tagsum(self, request, account, tag):
if not account or not tag:
return Response({'status': "need accoutn and tag"})
sum = Records.objects.filter(linkedaccount_id=account).filter(user_project=tag).aggregate(total=Sum('unblendedcost'))
return Response({'sum': sum['total']})
def get_queryset(self):
q = Q()
linkedaccount_id = self.request.query_params.get("linkedaccount_id") or ''
user_project = self.request.query_params.get("user_project") or ''
if linkedaccount_id:
q &= Q(linkedaccount_id=linkedaccount_id)
if user_project:
q &= Q(user_project=user_project)
return Records.objects.filter(q).annotate(Sum('unblendedcost'))
修改视图文件后调用$coreapi get http://localhost:8000/api/docs/
,看到:
...
records: {
list([page], [linkedaccount_id], [productname], [user_project])
tagsum(account, tag)
read(id, [linkedaccount_id], [productname], [user_project])
}
...
并且在调用该端点的一页加载之后,当我 运行 相同的命令时,我看到了这个输出:
...
records: {
tagsum: {
read(account, tag)
}
list([page], [linkedaccount_id], [productname], [user_project])
read(id, [linkedaccount_id], [productname], [user_project])
}
...
请注意,tagsum 方法现在嵌套了 read 方法。现在调用端点return出现以下错误。
errors.js:10 Uncaught LinkLookupError: Invalid link lookup: ["records","tagsum"]
at new LinkLookupError (errors.js:10)
at lookupLink (client.js:19)
at Client.action (client.js:38)
at eval (ProjectTagBilling.js:40)
at invokePassiveEffectCreate (react-dom.development.js:23482)
at HTMLUnknownElement.callCallback (react-dom.development.js:3945)
at Object.invokeGuardedCallbackDev (react-dom.development.js:3994)
at invokeGuardedCallback (react-dom.development.js:4056)
at flushPassiveEffectsImpl (react-dom.development.js:23569)
at unstable_runWithPriority (scheduler.development.js:468)
如有任何建议,我们将不胜感激。
解决方法,使用 fetch。
HTML
<script>
const csrftoken = document.querySelector('[name=csrfmiddlewaretoken]').value;
</script>
JS
useEffect(() => {
setIsLoading(true)
fetch(`/api/records/tagsum/${'432399220289'}/${props.tag}/`, {
headers: {'X-CSRFToken': csrftoken},
mehtod: 'GET'
}).then(response => response.json()).then(result => {
setIsLoading(false)
setSum(result.sum)
})
},[])
我在尝试获取 Django Rest Framework 提供的端点时收到 LinkLookupError
。此错误仅在单个成功请求后发生。我的 DRF view.py
看起来像:
class RecordViewSet(viewsets.ReadOnlyModelViewSet):
"""
API endpoint that allows Records to be viewed or edited.
"""
filter_backends = (SimpleRecordFilterBackend,)
serializer_class = RecordSerializer
permission_classes = (IsAuthenticated,)
@action(detail=False, url_path='tagsum/(?P<account>[^/.]+)/(?P<tag>[^/.]+)')
def tagsum(self, request, account, tag):
if not account or not tag:
return Response({'status': "need accoutn and tag"})
sum = Records.objects.filter(linkedaccount_id=account).filter(user_project=tag).aggregate(total=Sum('unblendedcost'))
return Response({'sum': sum['total']})
def get_queryset(self):
q = Q()
linkedaccount_id = self.request.query_params.get("linkedaccount_id") or ''
user_project = self.request.query_params.get("user_project") or ''
if linkedaccount_id:
q &= Q(linkedaccount_id=linkedaccount_id)
if user_project:
q &= Q(user_project=user_project)
return Records.objects.filter(q).annotate(Sum('unblendedcost'))
修改视图文件后调用$coreapi get http://localhost:8000/api/docs/
,看到:
...
records: {
list([page], [linkedaccount_id], [productname], [user_project])
tagsum(account, tag)
read(id, [linkedaccount_id], [productname], [user_project])
}
...
并且在调用该端点的一页加载之后,当我 运行 相同的命令时,我看到了这个输出:
...
records: {
tagsum: {
read(account, tag)
}
list([page], [linkedaccount_id], [productname], [user_project])
read(id, [linkedaccount_id], [productname], [user_project])
}
...
请注意,tagsum 方法现在嵌套了 read 方法。现在调用端点return出现以下错误。
errors.js:10 Uncaught LinkLookupError: Invalid link lookup: ["records","tagsum"]
at new LinkLookupError (errors.js:10)
at lookupLink (client.js:19)
at Client.action (client.js:38)
at eval (ProjectTagBilling.js:40)
at invokePassiveEffectCreate (react-dom.development.js:23482)
at HTMLUnknownElement.callCallback (react-dom.development.js:3945)
at Object.invokeGuardedCallbackDev (react-dom.development.js:3994)
at invokeGuardedCallback (react-dom.development.js:4056)
at flushPassiveEffectsImpl (react-dom.development.js:23569)
at unstable_runWithPriority (scheduler.development.js:468)
如有任何建议,我们将不胜感激。
解决方法,使用 fetch。
HTML
<script>
const csrftoken = document.querySelector('[name=csrfmiddlewaretoken]').value;
</script>
JS
useEffect(() => {
setIsLoading(true)
fetch(`/api/records/tagsum/${'432399220289'}/${props.tag}/`, {
headers: {'X-CSRFToken': csrftoken},
mehtod: 'GET'
}).then(response => response.json()).then(result => {
setIsLoading(false)
setSum(result.sum)
})
},[])