django - DRF Serializer Nested Field for User Creation -


so have custom model - profile, has onetoone relation django user object.

class profile(models.model):     user = onetoonefield(user, on_delete=models.cascade)     profile_type = models.charfield() 

i want make django rest framework serializer allows creation of creation , retrieval of user object's nested attributes "profile_type" attribute.

i want names specified on post request "username", "password", "email", etc. - instead of "profile_username", "profile_password", ...

so far have

class profileserializer(serializers.modelserializer):     username = serializers.charfield(source='profile_user_username')     password = serializers.charfield(source='profile_user_password')     email = serializers.charfield(source='profile_user_email')      class meta:         model = profile         fields = ('id',                   'profile_user_username', 'profile_user_password', 'profile_user_email',                   'username',                   'password',                   'email')         depth = 2 

but - i've been getting error:

improperlyconfigured: field name 'profile_user_username' not valid model 'profile' 

am getting syntax nested fields wrong? or else?

try:

class user(models.model):     username = models.charfield()     password = models.charfield()     email = models.charfield()  class userserializer(serializers.modelserializer):     class meta:         model = user  class profileserializer(serializers.modelserializer):     user = userserializer(many=true)     class meta:         model = profile         fields = ('user', 'profile_type',)      def create(self, validated_data):         user_data = validated_data.pop('user')         user = user.objects.create(**user_data)         profile = profile.objects.create(user=user, **validated_data)         return profile 

and check out this writable nested serializers.

for dealing nested object: checkout this


Comments