Python Django Rest框架:根据表字段值进行筛选
我正在用Python Django Rest框架:根据表字段值进行筛选,python,django,django-rest-framework,Python,Django,Django Rest Framework,我正在用Django restapi部分改进我的Django Web应用程序,根据筛选表字段值,我有一个问题 我的序列化程序类如下所示: class IndividuResearchSerializer(serializers.ModelSerializer) : class Meta : model = Individu fields = [ 'id', 'NumeroIdentification',
Django restapi
部分改进我的Django Web应用程序,根据筛选表字段值,我有一个问题
我的序列化程序类如下所示:
class IndividuResearchSerializer(serializers.ModelSerializer) :
class Meta :
model = Individu
fields = [
'id',
'NumeroIdentification',
'Nom',
'Prenom',
'VilleNaissance',
]
我的视图.py文件包含此类:
class IndividuResearchAPIView(ListAPIView) :
permission_classes = (IsAuthenticated,)
authentication_classes = (JSONWebTokenAuthentication,)
serializer_class = IndividuResearchSerializer
def get_queryset(self):
queryset = Individu.objects.all()
NIU = self.request.query_params.get('NumeroIdentification')
queryset = queryset.filter(NumeroIdentification=NIU)
return queryset
以及我的pythonic文件,该文件允许模拟从另一个基于API Rest的软件连接:
import requests
mytoken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoxLCJ1c2VybmFtZSI6IkFkbWluIiwiZXhwIjoxNTE5NzMxOTAxLCJlbWFpbCI6InZhbGVudGluQGRhdGFzeXN0ZW1zLmZyIiwib3JpZ19pYXQiOjE1MTk3MjgzMDF9.493NzJ4OUEzTKu5bZsZ9UafMwQZHz9pESMsYgfd0RLc"
url = 'http://localhost:8000/Api/Identification/search/'
NIU = "I-19312-00001-305563-2"
response = requests.get(url, NIU = NIU, headers={'Authorization': 'JWT {}'.format(mytoken)})
print(response.text)
我想在我的请求中输入一个NIU值
,以便筛选我的表并根据该NIU
返回对象
例如,在我的数据库中,我有以下对象:
由于我的API,我想返回这个对象,但我不知道我的函数get\u queryset
是否编写良好,以及如何编写API请求
在我的url.py文件中,我有:
url(r'^search/$', IndividuResearchAPIView.as_view() , name="Research"),
所以,我不做一个过滤的网址
我阅读这些帖子是为了获得更多元素:
显然,DRF文档:您需要使用此url进行筛选:http://localhost:8000/Api/Identification/search/?NumeroIdentification=NUA_value
。对于请求库,尝试使用params参数传递它:response=requests.get(url,params={'numeriodentification':NIU},headers={'Authorization':'JWT{}'。format(mytoken)}
如果我正确理解了您的问题,您需要使用此url进行筛选:http://localhost:8000/Api/Identification/search/?NumeroIdentification=NUA_value
。对于请求库,尝试使用params参数传递它:response=requests.get(url,params={'numeriodentification':NIU},headers={'Authorization':'JWT{}'。format(mytoken)}
是的,正是我所期望的!请将您的消息作为答案共享以验证它?