开发者

How to retain an added column in django queryset during serialization?

开发者 https://www.devze.com 2023-04-02 22:06 出处:网络
How do i retain my extra column i\'ve added dynamically on a queryset during serialization to a json string?

How do i retain my extra column i've added dynamically on a queryset during serialization to a json string?

this is my code:

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

# dynamica开发者_JAVA技巧lly 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 ????

What's happening, and how can i go round this?

Gath

NB: Am doing this because serializers don't follow relationships inside a model, just returns your current model.


It's more that the django serialiser looks at the columns, not at the attributes.

You can look at other serialisation tools: wad of stuff serializer springs to mind.

I wound up writing my own, that uses a django Form to serialise and deserialise.

0

精彩评论

暂无评论...
验证码 换一张
取 消