我有以下型号:
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')
我试图以我可以通过API endpoints 分配内容对象的方式序列化此模型
到目前为止,我已经这样做了:
class TaggedItemSerializer(serializers.ModelSerializer):
content_object = serializers.RelatedField(read_only=True)
class Meta:
model = TaggedItem
然而,这是只读 . 如果我删除了read_only参数,我必须为该字段指定查询集 . 但是,我有这种通用关系的许多不同的模型类型 . 如果我在序列化程序和模型中的其他地方指定所有可能的模型类型,似乎我正在复制代码 .
我也可以通过object_id和content_type字段设置内容对象,但是当我这样做时,我得到一个错误 .
例如:
{
...
object_id: 1,
content_type: 'auth.User'
}
使用 "detail": "JSON parse error - Expected object or value"
返回400响应
如何通过DRF API使这个content_object可写?
1 回答
像这样覆盖
.to_internal_value
,.validate
和.create
方法: