On Dec 28, 10:01 am, "Russell Keith-Magee" <freakboy3...@gmail.com>
wrote:
> On Sun, Dec 28, 2008 at 5:32 AM, adambossy <adambo...@gmail.com> wrote:
>
> > Russ,
>
> > Thanks for the reply. Specifically, I am wondering if there is some
> > feature of the Models API that allows me to retrieve the foreign key
> > object so that it is included in the serialized string. That is,
> > without writing my own serializer (which I suspect I may have to do).
>
> The serializer is decoupled from the Model API, so your question is
> unrelated to the model API. Serialization exploits the model metatdata
> embedded in models; the issue in this case is that the data extracted
> from the model by the serializer doesn't meet your requirements. You
> want to have a different serialization format (i.e., you want to roll
> out serialized objects in a different format to that provided by
> default). This means you will need to write your own serializer.
>
> Looking for deeper solutions to this problem -
>
> Ticket #4656 possibly describes part of your problem - this ticket
> describes an enhancement that would allow the serializer to follow.
> This would be much like the select_related() operator on querysets,
> but for serialization. This ticket hasn't seen any activity for a
> while, but it has been accepted for inclusion as soon as a working
> implementation is available.

The code attacted #4656 can be used as is for achieving the kind of
serialization that Adam requires. I'm am also using it quite
extensively in a large RESTful application. You can register my
serializer classes to replace the default Django json serializer. In
your settings.py add:

SERIALIZATION_MODULES = { 'json' : 'wadofstuff.serializers.json' }

Then in your own code from your example above

from django.core import serializers
print serializers.serialize('json', Template.objects.get(id=1),
relations=('type',))



{
        "pk": 1,
        "model": "template",
        "fields": {
            "type": {
                "pk": 3,
                "model": "type",
                "fields": {"name": "TemplateType3"}
        }
},


cheers

Matthew

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

Reply via email to