Django serializers and local/inherited model fields

1721 views
Skip to first unread message

psj

unread,
Dec 30, 2008, 2:27:45 PM12/30/08
to Django users
Django's base serializers only serialize a model's local fields, not
inherited ones, which seems odd at first blush (if an Employee is a
Person, and a Person has a name, wouldn't I be likely to want to see
the Employees' names by default?)

This looks straightforward to work around, but just wondering if
there's some gotcha I'm not thinking of here. Was this a performance
decision?

Thanks,
psj

Russell Keith-Magee

unread,
Dec 30, 2008, 9:06:11 PM12/30/08
to django...@googlegroups.com

This is one of the areas that reveals that serialization is a bit of a
'one-trick pony'. Serialization works really well for fixtures, but it
isn't particularly flexible when it comes to other serialization use
cases. In this case, the serializers are serializing exactly what the
database requires - the output is a JSON representation of the
database table contents.

The primary reason for this is the deserialization process. If you
include the 'Person' attributes on a serialized Employee, it becomes
more complex to determine whether you need to create a new Person
object when deserializing the Employee, and to disable creation of the
parent object instance when it isn't required. So - if you want to
fully serialize an Employee, you also need to serialize the underlying
Person object as well, and each model is responsible for serializing
their own attributes.

Expanding the usefulness of the serializers is something that we would
like to address. If you search the ticket database, you will find many
suggestions floating around for improving the capabilities of the
serialization process. However, it's not a trivial problem, and it
will take some serious design work. Any offers to help out in this
area would be gratefully accepted.

Yours,
Russ Magee %-)

Reply all
Reply to author
Forward
0 new messages