Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django serialization of inherited model

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.

like image 857
tefozi Avatar asked Jul 15 '09 18:07

tefozi


1 Answers

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/.

like image 130
Matt S. Avatar answered Oct 07 '22 21:10

Matt S.