Phyton Django serialization of inherited model python django serializers.serialize,python django json serializable

I have a problem with serialization of Django inherited models. For example

class Animal(models.Model):
    color = models.CharField(max_length=50)

class Dog(Animal):
    name = models.CharField(max_length=50)

...
# now I want to serialize Dog model with Animal inherited fields obviously included
print serializers.serialize('xml', Dog.objects.all())

and only Dog model has been serialized.

I can do smth like

all_objects = list(Animal.objects.all()) + list(Dog.objects.all())
print serializers.serialize('xml', all_objects)

But it looks ugly and because my models are very big so I have to use SAX parser and with such output it's difficult to parse.

Any idea how to serialize django models with parent class?

**EDIT: ** It use to work ok before this patch has been applied. And the explanation why the patch exist "Model saving was too aggressive about creating new parent class instances during deserialization. Raw save on a model now skips saving of the parent class. " I think there should be an option to be able to serialize "local fields only" by default and second option - "all" - to serialize all inherited fields.

Answer:1

You found your answer in the documentation of the patch.

all_objects = list(Animal.objects.all()) + list(Dog.objects.all())
print serializers.serialize('xml', all_objects)

However, if you change Animal to be an abstract base class it will work:

class Animal(models.Model):
    color = models.CharField(max_length=50)

    class Meta:
        abstract = True

class Dog(Animal):
    name = models.CharField(max_length=50)

This works as of Django 1.0. See http://docs.djangoproject.com/en/dev/topics/db/models/.

Answer: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

In order to use it on its own, you need to do:

serializer = AllFieldsSerializer()
serializer.serialize(queryset, fields=fields)
print serializer.getvalue()
Answer:3

Did you look at select_related() ? as in

serializers.serialize('xml', Dog.objects.select_related().all())
Answer:4

You can define a custom Serializer:

class DogSerializer(serializers.ModelSerializer):  
    class Meta:  
       model = Dog 
        fields = ('color','name') 

Use it like:
serializer = DogSerializer(Dog.objects.all(), many=True)
print serializer.data enter code here

Answer:5



  1. python django serializers.serialize
  2. python django json serializable

I have been trying to understand python metaclasses, and so have been going through some sample code. As far as I understand it, a Python metaclass can be any callable. So, I can have my metaclass ...

I have been trying to understand python metaclasses, and so have been going through some sample code. As far as I understand it, a Python metaclass can be any callable. So, I can have my metaclass ...

Is there any way to view a function's doc string when writing Python in VIM? For instance: def MyFunction(spam): """A function that foobars the spam returns eggs""" return foobar(spam)....

Is there any way to view a function's doc string when writing Python in VIM? For instance: def MyFunction(spam): """A function that foobars the spam returns eggs""" return foobar(spam)....