gpt4 book ai didi

python - 如何在序列化期间保留 django 查询集中添加的列?

转载 作者:行者123 更新时间:2023-12-01 06:07:44 24 4
gpt4 key购买 nike

如何保留在序列化为 json 字符串期间在查询集上动态添加的额外列?

这是我的代码:

tenant_unit = TenantUnit.objects.filter(unit__building__id=10)

# dynamically add a column unit_name
for tu in tenant_unit:
tu.unit_name = tu.unit.unit_name

# at this point if i loop thru the queryset( tenant_unit)
# i can see my dynamic column - unit_name

# build a json string
json_data = serializers.serialize("json", tenant_unit)

# check what's inside the json string, "mising" unit_name
print json_data # Can't find the unit_name ????

发生了什么事,我该如何解决这个问题?

迦特

注意:这样做是因为序列化器不遵循模型内部的关系,只是返回当前模型。

最佳答案

django 序列化程序更多地查看列,而不是属性。

你可以看看其他序列化工具:wad of stuff serializer浮现在脑海中。

我最终编写了自己的,它使用 django 表单来序列化和反序列化。

关于python - 如何在序列化期间保留 django 查询集中添加的列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7332258/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com