如何使用retrieveapiview在Django REST框架中基于特定字段检索数据?
有了以下代码,我能够根据ID
检索数据,如何更新此代码,以便可以基于文件名检索数据?
我的网址是
urlpatterns = [
path("items/<pk>", SingleUploadItem.as_view()),
]
我的观点是:
class SingleUploadItem(RetrieveAPIView):
queryset = fileUpload.objects.all()
serializer_class = fileUploadSerializer
我的模型是
class fileUpload(models.Model):
fileName = models.CharField(max_length=200, unique=True, blank=True)
With the code below, I am able to retrieve data based on id
, how can I update this code so I can retrieve data based on fileName instead?
My urls is
urlpatterns = [
path("items/<pk>", SingleUploadItem.as_view()),
]
My views is:
class SingleUploadItem(RetrieveAPIView):
queryset = fileUpload.objects.all()
serializer_class = fileUploadSerializer
My model is
class fileUpload(models.Model):
fileName = models.CharField(max_length=200, unique=True, blank=True)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,在urls.py
和views.py中
First, in urls.py
And in views.py,
是一个古老的帖子,但对于那些将来降落的人来说,无需手动做到这一点。 DRF检索方法将基于Lookup_field属性查找对象。默认情况下,这将其设置为“ PK”,但您可以将其设置为您在视图上想要的任何内容。对于此示例,将是:
视为
Is an old post but for those landing here in future, there is no need to manually do the get. DRF retrieve method will lookup the object based on the lookup_field attribute. By default this is set to 'pk' but you can set this to whatever you want on your view. For this example it would be:
View as