简体   繁体   English

如何在 Django Rest Framework 中使用 get_queryset 进行过滤?

[英]How to filter using get_queryset in Django Rest Framework?

Currently, the API I'm pulling is as such:目前,我正在提取的 API 是这样的:

http://127.0.0.1:8000/api/locs/data/2

and the output is as such:输出如下:

{
    "id": 2,
    "date": "2019-01-07",
    "hour": null,
    "measurement": null,
    "location": 6
}

What I want is actually filtering by the location value, so from the API url above, I would love to pull all the data that is from location: 2 .我想要的实际上是按location值过滤,所以从上面的 API url 中,我很想提取来自location: 2的所有数据。 How can I achieve that?我怎样才能做到这一点?

What I have so far:到目前为止我所拥有的:
views.py视图.py

class LocationDataView(viewsets.ModelViewSet):
    serializer_class = LocationDataSerializer
    permission_classes = [IsAuthenticated]
    authentication_classes = [TokenAuthentication]

    def get_queryset(self):
        queryset = LocationData.objects.all()
        pk = self.kwargs['pk']
        queryset = queryset.filter(location=pk)

    def perform_create(self, serializer):
        serializer.save()

and urls.pyurls.py

from django.urls import path, include
from rest_framework import routers
from . import views

router = routers.DefaultRouter()
router.register(r'locs', views.LocationListView, 'locs')
router.register(r'locs/data', views.LocationDataView, 'locs/data')

urlpatterns = [
    path('', include(router.urls)),

    path('locs/forecast-data/',
         views.getForecastData, name='forecast-data'),
]

My question is that, how can I access the pk for the location?我的问题是,如何访问该位置的pk

I think you need to add the retrieve function in the LocationDataView .我认为您需要在LocationDataView中添加retrieve功能。

class LocationDataView(viewsets.ModelViewSet):
    ...

    def retrieve(self, request, *args, **kwargs):
        instance = self.get_object()
        serializers = self.get_serializer(instance)
        return Response(serializers.data)

And in urls.py在 urls.py

...
router = routers.DefaultRouter()
router.register(r'locs', views.LocationListView, 'locs')
router.register(r'locs/<int:pk>', views.LocationDataView, 'locs/data')
...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django REST Framework - 从 get_queryset 返回一个值? - Django REST Framework - return a value from get_queryset? 如何在Django REST中使用外键字段过滤URL,正确覆盖get_queryset方法 - How to filter against URL with foreign key field in Django REST, overwriting get_queryset method correctly Django rest 壮观 - 自定义 get_queryset() - Django rest spectacular - Customizing get_queryset() pk上的Django get_queryset过滤器对象 - Django get_queryset filter objects on pk django get_queryset() 带条件过滤器() - django get_queryset() with conditional filter() Django REST框架:重写get_queryset()有时会返回一个doubled查询集 - Django REST Framework: overriding get_queryset() sometimes returns a doubled queryset Django 其余框架视图集 get_queryset() 不返回正确的查询集 - Django rest framework viewset get_queryset() doesn't return correct queryset Django Rest Framework错误无法在未设置`.queryset`或没有`.get_queryset()`方法的视图上应用DjangoModelPermissions - django rest framework error Cannot apply DjangoModelPermissions on a view that does not set `.queryset` or have a `.get_queryset()` method Django rest 框架,应该能够覆盖 get_queryset 而不是定义 queryset 属性吗? - Django rest framework, should be able to override get_queryset and not define queryset attribute? Django:get_queryset() 没有弹出 filter() 方法 - Django:get_queryset() is not popping up filter() method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM