问题
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
回答1:
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.
回答2:
You'll need a custom serializer to support inherited fields, as Django's serializer will only serialize local fields.
I ended up writing my own when dealing with this issue, feel free to copy it: https://github.com/zmathew/django-backbone/blob/master/backbone/serializers.py
https://github.com/zmathew/django-backbone/blob/351fc75797bc3c75d2aa5c582089eb39ebb6f19a/backbone/serializers.py
In order to use it on its own, you need to do:
serializer = AllFieldsSerializer()
serializer.serialize(queryset, fields=fields)
print serializer.getvalue()
回答3:
Maybe it is very late, but I give my solution, in case it could be useful. I used an other Django library that do the trick:
from django.forms.models import model_to_dict
model_to_dict(Location.objects.get(id=7), fields = ['name', 'address', 'phoneNumber'])
model_to_dict(Location.objects.get(id=7))
You can specify or not fields list. I've tried with a model of mine, and it worked fine for me. The only difference is that in the output you will get only the filed value, not the model information.
来源:https://stackoverflow.com/questions/2866105/json-serialization-of-a-django-inherited-model