Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 18732

Django Rest -api User registration

$
0
0

I've been trying to implement register method on UserView , to create new user each time I call Post request , But I got this error in postman

"detail": "Method "POST" not allowed."

views.py

class UserView(viewsets.ModelViewSet):    queryset = User.objects.all()    serializer_class = UserSerializers    @action(detail=True, methods=["post"])    def register(self, request):        serializer = UserSerializers(data=request.data)        if serializer.is_valid():            serializer.save()            return Response(serializer.data, status=status.HTTP_201_CREATED)        return Response(serializer.errors, status=status.HTTP_404_NOT_FOUND

even when I set detail=False , I got this error too ,even though I don't have any many-to-many relationship between any two models.

TypeError: Direct assignment to the forward side of a many-to-many set is prohibited. Use groups.set() instead.

Serializer.py

class UserSerializers(ModelSerializer):    class Meta:        model = User        fields = "__all__"        # exclude the 'password' field from the serialization        extra_kwargs = {"password": {"write_only": True}}    def create(self, validated_data):        user = User.objects.create_user(**validated_data)        Token.objects.create(user=user)        return user

Viewing all articles
Browse latest Browse all 18732

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>