reactivated
reactivated copied to clipboard
Pick doesn't serialise ids correctly for nested model properties
I found an issue with serialisation where parent id overrides children ids during serialisation.
Example
Assume I have the following 2 models and a property in the parent which extract data from the child:
class ChildInfo(TypedDict):
id: str
description: str
class ParentModel(models.Model):
@property
def children_info(self) -> List[ChildInfo]:
children = ChildModel.objects.filter(parent__pk=self.id)
return [ChildInfo(id=child.id, description=child.description) for child in children]
class ChildModel(models.Model):
parent = models.ForeignKey(
to=Parent,
verbose_name="linked [parent",
)
description = models.TextField()
Template:
@template
class View(NamedTuple):
parent: Pick[models. ParentModel, "id", "children_info"]
Now assume I have ParentModel (id:1) linked to two ChildModels (id:20 & id: 25)
When I print children_info
on django side I get the ids as expected:
[{'id': 20, 'description': 'test'}, {'id': 25, 'description': 'test2'}]
However, on client side the parent id overrides the children ids:
{
'id': 1,
'children_info': [{'id': 1, 'description': 'test'}, {'id': 1, 'description': 'test2'}]
}
Temporary Fix
Changing the definition of ChildInfo fixed the issue:
class ChildInfo(TypedDict):
version_id: str
description: str
And I correctly get the same output on both django and client side:
[{'version_id': 20, 'description': 'test'}, {'version_id': 25, 'description': 'test2'}]
Question
Is there an issue with serialisation when a name clash between ids happens?