0

I can't add a new user using Django Rest Framework. Here is my code from models.py:

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    password = models.CharField(max_length=15, default= None)
    first_name = models.CharField(max_length=100, validators=[name_validator])
    last_name = models.CharField(max_length=100, validators=[name_validator])
    email = models.CharField(max_length=100, validators=[mail_validator])
    created_at = models.DateTimeField(auto_now_add=True)

As you can see I am using models.OneToOneField cause I want to extend the default user to add some more fields.

Bellow is my serializers.py file:

class ProfileSerializer(serializers.ModelSerializer):
    class Meta:
        model = Profile
        permissions_classes = [
            permissions.AllowAny
        ]
        fields = '__all__'

The viewset is the following:

class UserViewset(viewsets.ModelViewSet):
    queryset = Profile.objects.all()
    serializer_class = ProfileSerializer

When I go to my endpoints and try to add a new user, I cannot put anything in the "user" field:

Click for image

I am a beginner and it would be of great help.

Thank you!

3
  • You need to have users before you can add it to Profile model. You can test this by adding some users before adding profile. Now when you add profile you will get all the available users for adding to the profile. Commented Dec 30, 2020 at 11:15
  • But What I want is to create a new user with all those attributes. Not to select from some existing users. Commented Dec 30, 2020 at 11:27
  • For that you need writable nested serializer. Another method is to override the create method of user serializer. Check this answer stackoverflow.com/a/29867704/4901118 Commented Dec 30, 2020 at 11:57

2 Answers 2

0

you can to this

from django.contrib.auth.models import User

class Profile(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    password = models.CharField(max_length=15, default= None)
    first_name = models.CharField(max_length=100, validators=[name_validator])
    last_name = models.CharField(max_length=100, validators=[name_validator])
    email = models.CharField(max_length=100, validators=[mail_validator])
    created_at = models.DateTimeField(auto_now_add=True)

serializer.py

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = "__all__"

class ProfileSerializer(serializers.ModelSerializer):
    user = UserSerializer(read_only=True)
    class Meta:
        model = Profile
        fields = '__all__'

views.py

class UserViewset(viewsets.ModelViewSet):
    queryset = Profile.objects.all()
    serializer_class = ProfileSerializer
    permission_classes = [permission. AllowAny,]
Sign up to request clarification or add additional context in comments.

1 Comment

Please explain how this code dump addresses the problem that the OP had?
0

When you want to extend the default user to add some more fields, the best way for that is substituting a custom User model by inheritance the AbstractUser model. Using this way, you can CRUD the user easily.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.