1
我想序列化一個多態模型,但只有他的基類型字段是序列化的,而不是多態的。Django - 序列化多態模型
models.py
class Folder(PolymorphicMPTTModel):
parent = PolymorphicTreeForeignKey('self', null=True, blank=True, related_name='children')
name = models.CharField(max_length=50)
class File(Folder):
srs_wkt = models.CharField(max_length=1000, blank=True, null=True)
views.py
from django.core import serializers
from django.core.serializers.json import DjangoJSONEncoder
file = File.objects.get(pk=1)
serialized = serializers.serialize('python', [file,])
response = json.dumps({'file':file}, cls=DjanJSONEncode)
return HttpResponse(response, content_type="application/json")
那怎麼我通常做連載我的模型對象,並將其作爲JSON,但在這裏,JSON對象僅具有srs_wkt字段,而不是名稱。
有沒有辦法序列化多態模型?