Python 如何在django rest框架中只获得主键的结果而不是所有结果?
在我的django应用程序中,我需要检查是否存在使用电话号码的用户。使用电话号码和OTP进行登录。 我可以通过get请求来检查用户是否存在Python 如何在django rest框架中只获得主键的结果而不是所有结果?,python,django,rest,django-rest-framework,django-rest-knox,Python,Django,Rest,Django Rest Framework,Django Rest Knox,在我的django应用程序中,我需要检查是否存在使用电话号码的用户。使用电话号码和OTP进行登录。 我可以通过get请求来检查用户是否存在 "/api/profiles/<primary key here>/". 我得到数据库中所有用户的列表 如果请求,我需要我的应用程序不返回任何内容 "/api/profiles/" "/api/profiles/<primary key here>/" 和用户详细信息(
"/api/profiles/<primary key here>/".
我得到数据库中所有用户的列表
如果请求,我需要我的应用程序不返回任何内容
"/api/profiles/"
"/api/profiles/<primary key here>/"
和用户详细信息(如果需要)
"/api/profiles/"
"/api/profiles/<primary key here>/"
网址:
观点:
class UserProfileViewSet(viewsets.ModelViewSet):
queryset = UserProfile.objects.all()
serializer_class = UserProfileSerializer
你能试试这个密码吗
from rest_framework import mixins
class UserProfileViewSet(viewsets.ViewSet, mixins.RetrieveModelMixin):
queryset = UserProfile.objects.all()
serializer_class = UserProfileSerializer
使用序列化程序执行此操作时,我找不到任何答案。 因此,我创建了一个基于函数的视图,使用表单数据接收电话号码,并在每次请求和用户存在时将密码更新为随机的6位otp 检查用户:
try:
UserProfiles.objects.get(phone_number=request.POST["phone_number"]
# generate otp
return HttpResponse(otp)
except:
return HttpResponse("user not found")
你能发布你的代码并添加序列化程序、视图和URL代码吗
try:
UserProfiles.objects.get(phone_number=request.POST["phone_number"]
# generate otp
return HttpResponse(otp)
except:
return HttpResponse("user not found")