0

I am creating user in django, on post save using reciever signal to save the Profile, I have profile model like this:

class Profile(models.Model):
    user = models.OneToOneField(base_settings.AUTH_USER_MODEL,on_delete=models.CASCADE)
    has_sharkpod = models.BooleanField("The users organization has a sharkpod", default=False)
    role = models.ForeignKey(Role, on_delete=models.DO_NOTHING, default=1)

    def __str__(self):
        return "Profile: " + self.user.username

This is my role model:

class Role(models.Model):
role_name = models.CharField(null=True, max_length=100) #unique = true
permissions = JSONField()
user = models.ManyToManyField(
    base_settings.AUTH_USER_MODEL, blank=True)
date = models.DateTimeField(auto_now=True)

Now I want to save role as well that is passed in the request body of user creation, But I am not able to do that.

This is my post save function for profile:

@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created: 
        Profile.objects.create(user=instance)

Update

My User ViewSet:

class CreateUserViewSet(viewsets.ModelViewSet):
authentication_classes = [JWTAuthentication]
permission_classes = [IsAdminUser, IsAuthenticated]
serializer_class = UserSerializer

def post(self, request, *args, **kwargs):
    serializer = self.get_serializer(data=request.data)
    serializer.is_valid(raise_exception=True)
    user = serializer.save()
    return Response({
        "user": UserSerializer(user, context=self.get_serializer_context()).data,

    })

And my user serializer:

class UserSerializer(serializers.ModelSerializer):
class Meta:
    model = User
    fields = ('username', 'email', 'first_name',
              'last_name', 'id', 'is_active', 'password', 'profile')
    read_only_fields = ('profile', 'is_active', )
def create(self, validated_data):
    user = User.objects.create_user(**validated_data)
    password = validated_data.pop('password')
    user.set_password(password)
    return user

I am Posting this data from postman:

 {
  "username": "testUser",
  "email": "bol@example.com",
  "first_name": "test",
  "last_name": "user",
  "password": "test21234",
  "role_id":"45"
}

User creation is working fine, Profile is also created. Now I want the role_id: 45 to be saved in the database. that is not happening.

  • Do you have any other signals in the project that work? If not your signals may not have registered correctly. See this post: https://stackoverflow.com/a/21612050/1268926 – Kedar Jun 10 '21 at 12:32
  • Please update question with the part of user creation and request. – intelis Jun 10 '21 at 12:32
  • The `post_save` signal is **not** passed the request. Your use case is better suited to adding the related `Profile` in your _view_ itself. – Abdul Aziz Barkat Jun 10 '21 at 12:42
  • Dear Kedar, Signal is working perfectly fine, I just want to save the role_id in the profile model but that is not happening, also How I will get the role id from request in create_user_profile() function ??? – Bilal Maher Jun 10 '21 at 12:58
  • I will update the question with full user part – Bilal Maher Jun 10 '21 at 12:59
  • Dear @AbdulAzizBarkat I am saving profile after the user creation, that is working, additionaly i want to save the role id as well, i dont know how to save role id in profile model. – Bilal Maher Jun 10 '21 at 13:01
  • @intelis I have updated my full question. – Bilal Maher Jun 11 '21 at 06:03

0 Answers0