I have the following model:
class TaggedItem(models.Model):
tag = models.SlugField()
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
I am trying to serialize this model in a way that I can assign the content object via an API endpoint
So far I have done this:
class TaggedItemSerializer(serializers.ModelSerializer):
content_object = serializers.RelatedField(read_only=True)
class Meta:
model = TaggedItem
However this is readonly. If I remove the read_only parameter, I must specify the queryset for the field. However, I have many different model types for this generic relationship. It seems like I am duplicating code if I specify all the possible model types both within the serializer and elsewhere in the model.
I could also set the content object through the object_id and content_type fields, but when I do this I get an error.
For example:
{
...
object_id: 1,
content_type: 'auth.User'
}
returns a 400 response with "detail": "JSON parse error - Expected object or value"
How can I make this content_object writable via the DRF api?