{"id":8003,"date":"2015-11-19T00:01:37","date_gmt":"2015-11-19T00:01:37","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/11\/19\/return-results-from-multiple-models-with-django-rest-framework-open-source-projects-tomchristie-django-rest-framework\/"},"modified":"2022-08-30T15:03:04","modified_gmt":"2022-08-30T15:03:04","slug":"return-results-from-multiple-models-with-django-rest-framework-open-source-projects-tomchristie-django-rest-framework","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/11\/19\/return-results-from-multiple-models-with-django-rest-framework-open-source-projects-tomchristie-django-rest-framework\/","title":{"rendered":"Return results from multiple models with Django REST Framework-open source projects tomchristie\/django-rest-framework"},"content":{"rendered":"<p>I have three models \u2014 articles, authors and tweets. I&#8217;m ultimately needing to use Django REST Framework to construct a feed that aggregates all the objects using the Article and Tweet models into one reverse chronological feed.<\/p>\n<p>Any idea how I&#8217;d do that? I get the feeling I need to create a new serializer, but I&#8217;m really not sure.<\/p>\n<p>Thanks!<\/p>\n<p><strong>Edit:<\/strong> Here&#8217;s what I&#8217;ve done thus far.<\/p>\n<p><em>app\/serializers.py:<\/em><\/p>\n<pre><code>class TimelineSerializer(serializers.Serializer):\n    pk = serializers.Field()\n    title = serializers.CharField()\n    author = serializers.RelatedField()\n    pub_date = serializers.DateTimeField()\n<\/code><\/pre>\n<p><em>app\/views.py:<\/em><\/p>\n<pre><code>class TimelineViewSet(viewsets.ModelViewSet):\n    \"\"\"\n    API endpoint that lists all tweet\/article objects in rev-chrono.\n    \"\"\"\n    queryset = itertools.chain(Tweet.objects.all(), Article.objects.all())\n    serializer_class = TimelineSerializer\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I have three models \u2014 articles, authors and tweets. I&#8217;m ultimately needing to use Django REST Framework to construct a feed that aggregates all the objects using the Article and Tweet models into one reverse chronological feed. Any idea how I&#8217;d do that? I get the feeling I need to create a new serializer, but [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-8003","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8003","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=8003"}],"version-history":[{"count":1,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8003\/revisions"}],"predecessor-version":[{"id":8707,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8003\/revisions\/8707"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=8003"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=8003"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=8003"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}