Using Django rest
Below is how I had my serializer.py.
class ProfileSerializer(serializers.ModelSerializer):
class Meta:
model = Profile
fields = ('id', 'name', 'active', 'type')
Type is a flatview
I then changed it so 'type' was nested for each profile like this...
class TypeSerializer(serializers.ModelSerializer):
class Meta:
model = Type
fields = ('id', 'name', 'active')
class ProfileSerializer(serializers.ModelSerializer):
type = TypeSerializer()
class Meta:
model = Profile
fields = ('id', 'name', 'active', 'type'')
now this works perfect, but I can now only update 'type' when in profile detail it's now read-only.
How can I add type when creating a new profile and still keep this nested view?
I hope I have explained this clearly .
UPDATE:
Ok, I just read this:
Note: Nested serializers are only suitable for read-only representations, as there are cases where they would have ambiguous or non-obvious behavior if used when updating instances. For read-write representations you should always use a flat representation, by using one of the RelatedField subclasses.
So that makes sense. So I changed it to....
type = serializers.PrimaryKeyRelatedField()
That puts it back in the POST and work, but it's a shame, can I not represent 'type' with ID and the name so it makes more sense to the end user?