0

I've encountered the following problem. Lets say I have a simple Profile Serializer and that is used as a nested serializer within the DevelopmentPLanItemSerializer, like so:

class SimpleProfileSerializer(serializers.ModelSerializer):
    profile_image = serializers.SerializerMethodField()

    class Meta:
        model = um.Profile
        fields = ('id', 'name', 'profile_image')

    def profile_image(self, obj):
        return obj.profile_image_url



class DevelopmentPlanItemSerializer(serializers.ModelSerializer):
    id = serializers.ModelField(
        model_field=dp.DevelopmentPlanItem()._meta.get_field('id'),
        required=False,
        allow_null=True
    )
    name = serializers.CharField(required=False, allow_null=True)        
    profile = SimpleProfileSerializer(required=False, allow_null=True)

    class Meta:
        model = dp.DevelopmentPlanItem
        fields = ('id', 'title', 'name', 'profile')

Now, I've market the 'profile' field with required=False, allow_null=True as this field is optional. However, when I save I still get an error because the fields within the SimpleProfileSerializer are not allowed to be empty. I could solve this by removing the nested serializer, so that I only POST / GET an Profile.id on the DevelopmentPlanItemSerialzer.profile field, but we really want the additional info from this field, not just the ID.

Is there a way to accomplish this? Where we, for example, supply only a Profile.PK on POST and get the entire nested serialized info on a GET request?

Jasper
  • 2,131
  • 6
  • 29
  • 61

1 Answers1

0

I solved using the links / methods that neverwalkaloner linked to in his comment, if anyone struggles with similar problems I recommend checking them out!

Jasper
  • 2,131
  • 6
  • 29
  • 61