【发布时间】:2018-04-16 03:10:55
【问题描述】:
我目前正在学习如何为我的公司构建 API。我已经很久没有这样做了,所以我现在基本上是一名初级开发人员。
按照一些教程,我使用基于类的视图启动并运行了我的 API。
我们使用硬件进行科学测量,它基于“运行”、“运行属性”和“腔室”。
对于 URL,我希望允许基于几个不同的关键字进行查找。因此,如果他们访问“www.example.com/api/runs_list”,API 将显示所有已完成的运行和已登录用户的会议室。
我想允许搜索特定的运行或按腔室。因此,如果他们想在 2017-11-02T18:20:24Z 开始运行,他们可以访问“www.example.com/api/2017-11-02T18:20:24Z”并获取与相关的 JSON 数据运行。
或者,如果他们访问“www.example.com/api/chamber_34-A”,他们将获得该室迄今为止执行的所有运行。
我查看了lookup_field 和lookup_url_kwargs,但它似乎只允许一个选项(我现在将其设置为chamber)。
这是我所拥有的:
urls.py
from django.conf.urls import url
from django.contrib import admin
from company.api.views import (
RunPropertiesListAPIView,
RunPropertiesDetailAPIView,
RunsDetailAPIView,
RunsListAPIView,
)
urlpatterns = [
url(r'^runs_list/$', RunsListAPIView.as_view(), name='runs_list'),
url(r'^properties_list/$', RunPropertiesListAPIView.as_view(),
name='properties_list'),
url(r'^runs_list/(?P<chamber>\d+)/$', RunsDetailAPIView.as_view(),
name='runs_list_detail'),
url(r'^properties_list/(?P<chamber>\d+)/$',
RunPropertiesDetailAPIView.as_view(), name='properties_list_detail'),
]
这里是 serializers.py
from rest_framework.serializers import ModelSerializer
from company.company_import.models import (
RunProperties,
Runs,
)
class RunsSerializer(ModelSerializer):
class Meta:
model = Runs
fields = [
'id',
'chamber',
'start_time',
'end_time',
'step_time'
]
class RunPropertiesSerializer(ModelSerializer):
class Meta:
model = RunProperties
fields = [
'id',
'runs',
'property_name',
'property_value'
]
这里是views.py
from rest_framework.generics import ListAPIView, RetrieveAPIView
from company.company_import.models import (
RunProperties,
Runs,
)
from company.company_import.api.serializers import (
RunPropertiesSerializer,
RunsSerializer,
)
class RunsListAPIView(ListAPIView):
queryset = Runs.objects.all()
serializer_class = RunsSerializer
class RunPropertiesListAPIView(ListAPIView):
queryset = RunProperties.objects.all()
serializer_class = RunPropertiesSerializer
class RunsDetailAPIView(RetrieveAPIView):
queryset = Runs.objects.all()
serializer_class = RunsSerializer
lookup_field = 'chamber'
lookup_url_kwarg = 'chamber'
class RunPropertiesDetailAPIView(RetrieveAPIView):
queryset = RunProperties.objects.all()
serializer_class = RunPropertiesSerializer
我尝试过的:
在 urls.py 上
(using a | to allow two options for RegEx)(didn't seem to work)
url(r'^runs_list/(?P<chamber>\d+)|(?P<start_time>\d+)/$',
RunsDetailAPIView.as_view(),
name='runs_list_detail'),
关于views.py
(Using python "or" to see if allowed for different options, didn't work)
class RunsDetailAPIView(RetrieveAPIView):
queryset = Runs.objects.all()
serializer_class = RunsSerializer
lookup_field = 'chamber' or 'start_time' (didn't work)
lookup_url_kwarg = 'chamber' or 'start_time' (didn't work)
(Using a list of strings to see if it allowed for different options, didn't
work)
queryset = Runs.objects.all()
serializer_class = RunsSerializer
lookups = ['chamber', 'start_time'] (didn't work)
lookup_field = lookups (didn't work)
lookup_url_kwarg = lookups (didn't work)
目前我已将其默认设置为分庭,但我真的很想启用特定且多样化的搜索查找。
【问题讨论】:
标签: python django django-rest-framework django-views