简体   繁体   中英

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 ? Please also suggest the modifications needed to make this possible and enable API to lookup by email like users/email.

Set the lookup_field and lookup_url_kwarg on your view or viewset that subclasses GenericAPIView . A basic example using a ModelViewSet and a SimpleRouter would look like this:

views.py:

class UserViewSet(viewsets.ModelViewSet):
    lookup_field = 'email'
    lookup_url_kwarg = 'email'

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.

class UserSerializer(serializers.HyperlinkedModelSerializer):

    class Meta:
        model = User
        fields = ('url', 'username')
        lookup_field = 'email'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM