2010-05-19 65 views
4

我有以下的Django模型JSON序列化繼承的模型

class ConfigurationItem(models.Model): 

    path = models.CharField('Path', max_length=1024) 
    name = models.CharField('Name', max_length=1024, blank=True) 
    description = models.CharField('Description', max_length=1024, blank=True) 
    active = models.BooleanField('Active', default=True) 
    is_leaf = models.BooleanField('Is a Leaf item', default=True) 

class Location(ConfigurationItem): 

    address = models.CharField(max_length=1024, blank=True) 
    phoneNumber = models.CharField(max_length=255, blank=True) 
    url = models.URLField(blank=True) 
    read_acl = models.ManyToManyField(Group, default=None) 
    write_acl = models.ManyToManyField(Group, default=None) 
    alert_group= models.EmailField(blank=True) 

完整的模型文件here是否有幫助。

您可以看到Company是ConfigurationItem的子類。

我想使用JSON序列化使用django.core.serializers.serializer或WadofStuff序列化程序。

兩個串行給我同樣的問題...

>>> from cmdb.models import * 
>>> from django.core import serializers 
>>> serializers.serialize('json', [ ConfigurationItem.objects.get(id=7)]) 
    '[{"pk": 7, "model": "cmdb.configurationitem", "fields": {"is_leaf": true, "extension_attribute_10": "", "name": "", "date_modified": "2010-05-19 14:42:53", "extension_attribute_11": false, "extension_attribute_5": "", "extension_attribute_2": "", "extension_attribute_3": "", "extension_attribute_1": "", "extension_attribute_6": "", "extension_attribute_7": "", "extension_attribute_4": "", "date_created": "2010-05-19 14:42:53", "active": true, "path": "/Locations/London", "extension_attribute_8": "", "extension_attribute_9": "", "description": ""}}]' 
>>> serializers.serialize('json', [ Location.objects.get(id=7)]) 
    '[{"pk": 7, "model": "cmdb.location", "fields": {"write_acl": [], "url": "", "phoneNumber": "", "address": "", "read_acl": [], "alert_group": ""}}]' 
>>> 

的問題是序列化的公司模型只給我直接與模型相關的領域,而不是從它的父對象的字段。

有沒有改變這種行爲的方法,或者我應該看看構建對象的字典並使用simplejson來格式化輸出?

在此先感謝

〜SM

回答

4

這是那個時代當答案可能來得太晚了原始海報之一,但可能會派上用場的下一個Google員工。

如果您需要顯着更高級的序列化,我不能幫助您,但是如果您只想優雅地處理多表繼承,那麼需要查看的地方位於:django/core/serializers/base.py的基類Serializer

serialize方法有一行:

for field in concrete_model._meta.local_fields:

的Monkeypatching或重寫這個類&代替該符合:

for field in concrete_model._meta.fields:

有一些注意事項需要注意的但是,看到Django的Git的回購承諾12716794分貝&這兩個問題:

https://code.djangoproject.com/ticket/7350

https://code.djangoproject.com/ticket/7202

長話短說,你也許應該謹慎行事這在全球範圍內,儘管壓倒性的行爲可能會取決於你的目標。

+0

非常感謝!如果有人試圖按照這個建議拷貝自己的''base.py'',''python.py''和其中一個具體的序列化器(json,xml等)。然後將該行更改爲''base.py''中建議的@philipk,並將繼承鏈覆蓋到具體的序列化程序。您還可以將新的序列化程序添加到設置中,查看'__init __。py''(在django序列化程序包中)以獲取更多詳細信息。再次感謝@philipk,你救了我的一天! :-) – Nagasaki45 2014-06-17 10:14:40

2

您需要一個自定義序列化程序來支持繼承字段,因爲Django的序列化程序只會序列化本地字段。

我結束了處理這個問題的時候寫我自己的,隨意複製:https://github.com/zmathew/django-backbone/blob/master/backbone/serializers.py

https://github.com/zmathew/django-backbone/blob/351fc75797bc3c75d2aa5c582089eb39ebb6f19a/backbone/serializers.py

爲了使用它自己的,你需要做的:

serializer = AllFieldsSerializer() 
serializer.serialize(queryset, fields=fields) 
print serializer.getvalue() 
+1

現在鏈接已損壞.. – abrunet 2015-11-06 14:24:51