0

I am using the Django default User model to do user registration. When I was trying to create a user I got this error message. Direct assignment to the forward side of a many-to-many set is prohibited. Use groups.set() instead.

# views.py

from django.contrib.auth.models import User
from rest_framework import viewsets
from app.serializers import UserSerializer

# Create your views here.


class UserViewSet(viewsets.ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer


# serializers.py

from django.contrib.auth.models import User
from rest_framework import serializers


class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = '__all__'
        extra_kwargs = {'password': {'write_only': True, 'required': False}}

    def create(self, validated_data):
        print(validated_data)
        user = User.objects.create_user(**validated_data)
        return user

# urls.py

from django.urls import path, include
from rest_framework import routers
from app import views
from rest_framework.authtoken.views import obtain_auth_token

router = routers.SimpleRouter()
router.register(r'users', views.UserViewSet)
urlpatterns = [
    path('auth/', obtain_auth_token),
]
urlpatterns += router.urls

I only insert the user record, there is no foreign table or many-to-many relationship. Not sure why the system throws this error.

Attached with the postman screenshot. enter image description here

Einn Hann
  • 651
  • 2
  • 8
  • 17
  • Can you please show what you have in `print(validated_data)`? – Sunderam Dubey Aug 21 '22 at 04:40
  • {'password': 'password', 'is_superuser': False, 'username': 'dba', 'first_name': 'John', 'email': 'dba@hotmail.com', 'is_staff': False, 'is_active': False, 'groups': [], 'user_permissions': []} – Einn Hann Aug 21 '22 at 06:15
  • If I comment off the def create(self, validated_data): inside the UserSerializer it can be inserted without error. – Einn Hann Aug 21 '22 at 06:16
  • Yes, I think it's by default. – Sunderam Dubey Aug 21 '22 at 06:16
  • https://stackoverflow.com/questions/50015204/direct-assignment-to-the-forward-side-of-a-many-to-many-set-is-prohibited-use-e Have you tried from this? – Sunderam Dubey Aug 21 '22 at 06:17
  • Try which one? Because I only got one model – Einn Hann Aug 21 '22 at 06:19
  • Does the `first_name`, `last_name` and `email` is coming all the time? – Sunderam Dubey Aug 21 '22 at 06:34
  • Try `User.objects.create_user(username=validated_data['username'], email=validated_data['email'], password=validated_data['password'],**validated_data)` or simply remove `**validated_data` at the end for extra fields argument, refer [GitHub](https://github.com/django/django/blob/e9fd2b572410b1236da0d3d0933014138d89f44e/django/contrib/auth/models.py#L158). – Sunderam Dubey Aug 21 '22 at 06:46
  • try to specify only required eg. ***(username, password, first_name, email)*** fields instead of `__all__` – Ankit Tiwari Aug 21 '22 at 07:59

0 Answers0