views:

54

answers:

1

Hello,

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

A: 

I am currently working at a serializer that would cover this feature. If you still have no answer in ~one week, I can come back to you and give you the google code link to download it.

sebpiq
Reading about serializing models* I see that this is expected behaviour, but the workaround ( all_objects = list(queryset) + list(queryset) ) looks very clumsy.. Must be a better way* http://docs.djangoproject.com/en/dev/topics/serialization/?from=olddocs#inherited-models
Simon Morris
Yeah yeah ! I know it is the expexted behaviour. In one of my projects, I had the need for 'more evolved' serialization, supporting in particular 'deep' (de)/serialization with natural keys ; (de)/serialization of properties ; and (de)/serialization with multi-table inheritance.Anyway, as you pointed it, it is the expected behaviour, so I don't think there is any other way to do it. In my opinions the two solutions are : 1. Finding some ugly hack2. Finding a library that does it for you
sebpiq
Hi Sebastien,Can I have a look at the serialization library you mentioned that might do this?Thanks
Simon Morris
Hello ! I am currently remodelling the whole stuff, to have more like a real serialization framework. I think, little by little I am coming to something nice, but this is not ready to use. However, you can check the homepage (http://code.google.com/p/django-serializable/) periodically if it still interests you, when this will be ready there will be a featured download ! I don't promise anything, but I think that in 1-2 weeks, you will be able to use it to fully serialize a model. And of course, if you like the code and want to help, feel free to send me an email !
sebpiq