[Fixed]-Serialize a Django model with a ForeignKey

1👍

You are setting the serializer field to read_only, which means it will not be used when creating or updating an object, only when reading it.

Read-only fields are included in the API output, but should not be included in the input during create or update operations. Any ‘read_only’ fields that are incorrectly included in the serializer input will be ignored. Set this to True to ensure that the field is used when serializing a representation, but is not used when creating or updating an instance during deserialization.

http://www.django-rest-framework.org/api-guide/fields/#read_only

Also, try using PrimaryKeyRelatedField instead of RelatedField. See the documentation for the field.

Leave a comment