title: Serializing Foreign Key objects in Django
body: <p>I have been working on developing some RESTful Services in Django to be used with both Flash and Android apps. </p>
  
  <p>Developing the services interface has been quite simple, but I have been running into an issue with serializing objects that have foreign key and many to many relationships.</p>
  
  <p>I have a model like this:</p>
  
  <pre><code>class Artifact( models.Model ):
      name                = models.CharField( max_length = 255 )
      year_of_origin      = models.IntegerField( max_length = 4, blank = True, null = True )
      object_type         = models.ForeignKey( ObjectType, blank = True, null = True )
      individual          = models.ForeignKey( Individual, blank = True, null = True )
      notes               = models.TextField( blank = True, null = True )
  </code></pre>
  
  <p>Then I would perform a query on this model like this, using <code>select_related()</code>, to be sure that foreign key relationships are followed:</p>
  
  <pre><code>artifact = Artifact.objects.select_related().get(pk=pk)
  </code></pre>
  
  <p>Once I have the object, I serialize it, and pass that back to my view:</p>
  
  <pre><code>serializers.serialize( "json", [ artifact ] )
  </code></pre>
  
  <p>This is what I get back, note that the foreign keys (object_type and individual) are just the id's to their related objects.</p>
  
  <pre><code>[
        {
              pk: 1
              model: "artifacts.artifact"
              fields: {
                  year_of_origin: 2010
                  name: "Dummy Title"
                  notes: ""
                  object_type: 1
                  individual: 1
              }
        }
  ]
  </code></pre>
  
  <p>This is great, but what I was hoping for when using <code>select_related()</code> was that it would automatically populate the foreign key fields with the related object, not just the object's id.</p>
  
  <p>I am recent convert to Django, but put in a fair amount of time developing with CakePHP. </p>
  
  <p>What I really like about the Cake ORM was that it would follow the relationships and create nested objects by default, with the ability to unbind the relationships when you were calling your query. </p>
  
  <p>This made it very easy to abstract the services in a way that did not require any intervention on a case by case basis.</p>
  
  <p>I see that Django does not do this by default, but is there a way to automatically serialize an object and all of it's related objects? Any tips or reading would be much appreciated.</p>