I have a model that references a Generic Relation that I want to serialize in a detailed manner.
class AType(models.Model):
foo = CharField()
class BType(models.Model):
bar = PositiveIntegerField()
class ToSerialize(models.Model):
scope_limit = models.Q(app_label="app", model="atype") | \
models.Q(app_label="app", model="btype")
content_type = models.ForeignKey(ContentType, limit_choices_to=scope_limit)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
I'd like the JSON for the list method of the ToSerialize viewset to look like:
[
{
"atype": { "id": 1, "foo": "a" }
},
{
"atype": { "id": 2, "foo": "b" }
},
{
"btype": { "id": 1, "bar": "1" }
},
{
"btype": { "id": 2, "bar": "2" }
}
]
Is there a way I can have the serializer for the ToSerialize object's viewset produce "conditional fields" based on the content_type/object_id that will achieve this effect?