[Answered ]-Select_Related and JSON – How to serialize foreign key objects

1👍

Edit

From https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.select_related, you can try like this:

e = Entry.objects.select_related('blog').filter(...)
return serializers.serialize('json', [x.blog for x in e])
👤ruddra

1👍

Thanks to @bento who suggested the following answer, which is available at django serialize foreign key objects:

entries = Entry.objects.select_related('blog').filter(...)
list = []
for row in entries:
        list.append({'blog_title':row.blog.title, 'entry_title': row.title})
return json.dumps(list)

Leave a comment