简体   繁体   English

如何在 django rest API 中为路由器提供参数?

[英]How to provide a parameter to a router in django rest API?

So the url I am trying to achieve looks like this:所以我试图实现的网址如下所示:

127.0.01:8000/api/tech/?belongs=id

My router looks like this:我的路由器看起来像这样:

router = routers.DefaultRouter()
router.register('tech', TechViewSet, basename="tech")

urlpatterns = [
    path('', include(router.urls)),
    re_path(r'^tech/(?P<belongs>)$', include(router.urls), name="info"),

My viewset looks like this (Also has a retrieve and list functions):我的视图集看起来像这样(还有一个检索和列表功能):

@action(detail=True, url_path='^tech/(?P<belongs>)$', methods=['get'])
    def retrieve1(self, request, group=None):
          pass

And the router is obviously included in urls.py of main project并且路由器显然包含在主项目的 urls.py 中

How to get this url working.. 127.0.01:8000/api/tech/?belongs=id如何让这个网址工作.. 127.0.01:8000/api/tech/?belongs=id

Please help.请帮忙。 and Im sorry, I'm still learning and the routing part is confusing..对不起,我还在学习,路由部分令人困惑..

Thankyou so much非常感谢

It's a little tricky without knowing your model structure.在不知道您的模型结构的情况下,这有点棘手。 But if you're using your get parameters for filtering you can use django_filters to do the heavy lifting for you.但是,如果您使用get参数进行过滤,则可以使用django_filters为您完成繁重的工作。 Something like this:像这样的东西:

pip install django-filter

add this to your rest framework settings: 'DEFAULT_FILTER_BACKENDS':['django_filters.rest_framework.DjangoFilterBackend']将此添加到您的休息框架设置: 'DEFAULT_FILTER_BACKENDS':['django_filters.rest_framework.DjangoFilterBackend']

Then in your TechViewSet you can add filterset_fields :然后在您的TechViewSet您可以添加filterset_fields

class TechViewSet:
    <your other variables>
    filterset_fields = ['belongs',]

you can then add query parameters ?belongs=<some_id> to your url and your results will be filtered.然后,您可以将查询参数?belongs=<some_id>到您的网址,您的结果将被过滤。

docs: https://www.django-rest-framework.org/api-guide/filtering/#djangofilterbackend文档: https : //www.django-rest-framework.org/api-guide/filtering/#djangofilterbackend

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

相关问题 如何在django rest框架路由器中替换或编辑lookup参数? - How to replace or edit the lookup parameter in a django rest framework Router? 如何提供API以获取django-rest中任何模型进行的更改的历史记录? - How can I provide an API to get the History of changes made in any model in django-rest? 在使用带Django REST Framework的路由器时,如何让APIViews和ViewSets显示在API Root上? - How to get APIViews and ViewSets to show on API Root when using Router with Django REST Framework? 分层路由器 Django Rest - Hierarchical Router Django Rest 如何为 django rest 框架 DecimalField 序列化器提供选择? - How to provide choices to django rest framework DecimalField serializer? 如何在“ PUT”请求中提供其他数据以在Django REST Framework中进行更新? - How to provide additional data in “PUT” request for update in Django REST Framework? Django 不为 AnonymousUser 提供 DB 表示 - Django rest Z8A5DA52ED12064417D8AAZE700064417D8AAZE70C - Django doesn't provide a DB representation for AnonymousUser- Django rest api REST API:如何提供 http 请求标头详细信息 - REST API: How to provide http request header details 如何向内联网计算机提供Rest Framework API? - How can I provide the Rest Framework API to intranet machine? 如何根据API参数将Django查询集派生到REST请求? - How can I derive a Django queryset to a REST request based on API parameter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM