[英]How to delete a User with particular email using Django REST framework
I am following the quick Quick Start Tutorial( http://www.django-rest-framework.org/tutorial/quickstart#quickstart ) Its possible to create/delete/update a user in database if we know its "id", But is it possible to do the same for a user with particular email ? 我正在遵循快速快速入门教程( http://www.django-rest-framework.org/tutorial/quickstart#quickstart )如果知道数据库的“ id”,则可以在数据库中创建/删除/更新用户,但是有特定电子邮件的用户可以这样做吗? Please also suggest the modifications needed to make this possible and enable API to lookup by email like users/email.
请同时提出必要的修改,以实现此功能并使API能够通过电子邮件(如用户/电子邮件)进行查找。
Set the lookup_field
and lookup_url_kwarg
on your view or viewset that subclasses GenericAPIView
. 在子类
GenericAPIView
视图或视图集上设置lookup_field
和lookup_url_kwarg
。 A basic example using a ModelViewSet
and a SimpleRouter
would look like this: 使用一个基本的例子
ModelViewSet
和SimpleRouter
是这样的:
views.py: views.py:
class UserViewSet(viewsets.ModelViewSet):
lookup_field = 'email'
lookup_url_kwarg = 'email'
urls.py: urls.py:
router = routers.SimpleRouter()
router.register(r'^users', UserViewSet)
urlpatterns = router.urls
If you are using HyperlinkedModelSerializer
, you must set lookup_field
in the serializer too. 如果您正在使用
HyperlinkedModelSerializer
,则还必须在序列化器中设置lookup_field
。
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ('url', 'username')
lookup_field = 'email'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.