I wanted to get the communities thoughts on this subject.  I am
working on a simple site that has news articles, each of which has a
reference to a User object provided by django.contrib.auth that is the
author of the news article.  This is done right now using the
following code:

class NewsArticle(models.Model):
    subject = models.CharField(max_length=1024)
    author = models.ForeignKey(User)
    body = models.TextField()
    publish_date = models.DateTimeField('date published')

As expected, the SQL produced stores the key of the User table in the
News table.

However, when you try to serialize this with the built-in JSON
serializer, it doesn't traverse the objects.  Instead, what you get is
something like this:

[
  {"pk": 1,
   "model": "news.newsarticle",
   "fields": {
      "body": "This is my very first news article!\r\nIt has some news
stuff in it",
      "author": 1, "publish_date": "2009-02-09 16:09:22",
      "subject": "My first news article!"
   }},
   {"pk": 2,
   "model": "news.newsarticle",
   "fields": {
      "body": "Some more news for you...",
      "author": 1,
      "publish_date": "2009-02-11 08:08:36",
      "subject": "Some more news for you"
     }
}]

Notice author just simply prints out the ID.  This is due to the lazy-
nature of the serializers.  I have done some reading on the Internets,
and from what I can tell there are a few options here:
  1) Write a custom serializer to do this.  There are some examples
       out there, but the down side is you have to support the code
       yourself.  Compatibility with client-side libraries might be a
factor.
  2) Make multiple calls...one to get the News, then one to get each
      author.  This could get expensive and cause the interface to
feel
      sluggish since calls to the backend would grow exponentially.

I wanted to get the general consensus of how others have tackled this
problem, and to see if there are other angles/avenues I have yet to
consider.
--~--~---------~--~----~------------~-------~--~----~
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