Hi Russ,

Thanks for the suggestions once again, I've thought of changing the
model for handling nested fields.

Each model can have a no of serializers, and they can be plugged in to
other serializers
and in this way nested models can be handled instead of cycling
through a tree of arbitrary depth.
Each serializer object would have a ``dump_object()`` method that
would return a python object.
This method is called when a serializer is plugged into another in a
nested model.

>My point is that it should be *possible* to define a "generic"
>serialization strategy -- after all, that's what Django does right
>now. If arguments like this do exist, they should essentially be
>arguments used to instantiate a specific serialization strategy,
>rather than something baked into the serialization API.

Yes, I'll be adding arguments ``attrs`` and ``exclude`` to the
__init__ method of the serializer.

Here is how the API can be used for generating the formats in those 2
examples:

"""
For the first option.
"""

class OriginalOutput(JSONSerializer):
    wrap_fields = "fields"
    indent = 4

    def meta2_pk(self, obj):
        #get pk
    def meta2_model(self, obj):
        #get model name

"""
The second option.
"""

class Base(JSONSerializer):
    """
    A base serializer with formatting options.
    """
    indent = 4

class Editor(Base):
    """
    This can be used to serialize a Person model.
    Note the company field, if no such Serializer object is specified,
the foreign key is returned.

    By specifying the ``attrs`` argument while initializing the
object, one can restrict the fields
    being serialized. Otherwise, all available fields and metadata
will be serialized.
    """
    company = Company(from_field = 'company', attrs = ['name',
'founded'])

class Author(Base):
    """
    The dump_object method is called when a serializer is plugged into
another and returns a python object,
    this is normally a dict but this can be overridden.
    """
    def dump_object(self, obj):
        return obj.first_name + ' ' + obj.last_name


class BookDetails(Base):
    """
    This is the serializer that will yield the final output.

    Since the 'authors' field is M2M, the Author serializer will be
applied over the list of authors.

    Aliases is a dict that maps attribute names to their labels during
serialization.

    """
    wrap_all = "book details"
    authors = Author(from_field = 'authors')
    editor = Editor(from_field = 'editor', attrs = ['firstname',
'lastname', 'coworkers', 'phone', 'company'])
    aliases = {
        'title': 'book_title',
        'acount': 'author_count'
    }

    def meta2_acount(self, obj):
        # return count of authors


The serialized data can be obtained by calling BookDetails.serialize()
or OriginalOutput.serialize().

Some of the options I didn't cover in the above example are:

* A ``fieldset`` option, which is a list of fields to be serialized.
If it is not set, all fields are part of the
serializer. This can be additionally restricted during initialization
as shown above.

* Reverse relations can be used in the same way as other pluggable
serializers by
specifying the ``from_field`` argument as the related name during
initialization.

I hope this will add more flexibility.

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-developers@googlegroups.com.
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.

Reply via email to