Django继承模型的JSON序列化 [英] JSON Serialization of a Django inherited model

查看:118
本文介绍了Django继承模型的JSON序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下Django模型

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)

完整的模型文件为在这里(如果有帮助的话).

您可以看到Company是ConfigurationItem的子类.

我正在尝试使用django.core.serializers.serializer或WadofStuff序列化器使用JSON序列化.

两个序列化器都给我同样的问题...

>>> 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": ""}}]'
>>>

问题在于,序列化Company模型只会给我直接与该模型相关联的字段,而不是来自其父对象的字段.

是否有改变这种行为的方法,还是我应该在构建对象字典并使用simplejson格式化输出格式?

预先感谢

〜sm

解决方案

在这种情况下,对于原始发帖人来说答案可能为时已晚,但对于下一位Googler而言可能会派上用场.

如果您需要更高级的序列化,那么我无济于事,但是如果您只想优雅地处理多表继承,则可以在以下位置找到它:django/core/serializers/base.pySerializer基类上. /p>

serialize方法中有一行:

for field in concrete_model._meta.local_fields:

Monkeypatching或覆盖该类&将该行替换为:

for field in concrete_model._meta.fields:

但是,有一些注意事项需要注意,请参阅Django Git回购&中的commit 12716794db.这两个问题:

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

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

长话短说,尽管您可以根据自己的目标来覆盖行为,但您最好在全球范围内谨慎行事.

I have the following Django models

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)

The full model file is here if it helps.

You can see that Company is a child class of ConfigurationItem.

I'm trying to use JSON serialization using either the django.core.serializers.serializer or the WadofStuff serializer.

Both serializers give me the same problem...

>>> 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": ""}}]'
>>>

The problem is that serializing the Company model only gives me the fields directly associated with that model, not the fields from it's parent object.

Is there a way of altering this behaviour or should I be looking at building a dictionary of objects and using simplejson to format the output?

Thanks in advance

~sm

解决方案

This is one of those times where the answer may come too late for the original poster, but might come in handy for the next Googler.

If you need significantly more advanced serialization, I can't help you, but if you only want graceful handling of multi-table inheritance, the place to look is in: django/core/serializers/base.py at the Serializer base class.

In the serialize method there is a line:

for field in concrete_model._meta.local_fields:

Monkeypatching or overriding that class & replacing that line with:

for field in concrete_model._meta.fields:

There are some caveats to be aware of however, see commit 12716794db in the Django Git repo & these two issues:

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

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

Long story short, you probably should be careful about doing this globally, though overriding that behavior may be fine depending on your goal.

这篇关于Django继承模型的JSON序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆