0
votes

I have 2 models, Account model as shown

class Account(AbstractBaseUser,PermissionsMixin):
    email = models.EmailField(max_length=100, unique = True)
    username = models.CharField(max_length = 30)
    is_admin = models.BooleanField(default = False)
    is_staff = models.BooleanField(default = False)
    is_active = models.BooleanField(default = False)
    date_joined = models.DateTimeField(auto_now_add = True)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = [
       'username'
    ]

and the profile table as shown below

class Profile(models.Model):
    user = models.OneToOneField(Account, on_delete = models.CASCADE)
    avatar = models.ImageField(default = "defaultpic.png", blank = True, null = True, upload_to="MEDIA_FILES" )
    tel = PhoneField(blank=False, null = False)

ProfileSerializer

class ProfileSerializer(serializers.ModelSerializer):
class Meta:
    model = Profile
    fields = [
        'avatar',
        'tel',
    ]

UserSerializer

class AccountSerializer(serializers.ModelSerializer):
profile = ProfileSerializer(read_only = True)

class Meta:
    model = get_user_model()
    fields = [
        'email',
        'username',
        'password',
        'profile'
    ]

    extra_kwargs = {
'password': {'write_only': True},
}

def create(self, validated_data):
    user_password = validated_data.pop('password')
    user = get_user_model()(
    email = validated_data['email'],
    username = validated_data['username']
    )
    user.set_password(user_password)
    user.save()

    user.email_user("Hello world")

    return user

Endpoint to register a new user

@api_view(["POST"]) @permission_classes([AllowAny, ])

def register(request): profSerializer = ProfileSerializer( data = request.data ) serializer = AccountSerializer( data = request.data )

if serializer.is_valid():
    serializer.save()
    serializer.send_mail("Hello")
    return JsonResponse( serializer.data )

else:
    print( serializer.errors )
    return JsonResponse(  { "_err": serializer.errors }  )

My problem now comes in when I send a post request to the registering endpoint, the profile fields remain empty, the avatar field is assigned the default value of default.png. I need the endpoint to create both user account and profile entries whenever a user sign up

The post request to the endpoint is the following JSON

{
      email:"some email",
      username:"username",
      password:"password",
      tel:"tel number",
      avatar:"image"
    }