Django: Order objects based on likes for popular feed











up vote
0
down vote

favorite
1












I want to implement 'Popular' Feed. I want to show the series of posts which has the likes the most.



Here is abstract model schema.



(Model) Like 
user = User
content_type = Post, User, Group
object_id = 22

(Model) Post
user = User
content = ''
created_at = '12082017'


In views.py, I want to get sorted queryset like,



 Post.objects.order_by('-like')[:30] 


Since my Post model doesn't have like field, I can't use order_by.



I'm thinking about adding score field to track of likes and current time since then. Do you think it will work? :)



Also any thought or advice on how to make popular feed will be highly appreciated. Thank you










share|improve this question




























    up vote
    0
    down vote

    favorite
    1












    I want to implement 'Popular' Feed. I want to show the series of posts which has the likes the most.



    Here is abstract model schema.



    (Model) Like 
    user = User
    content_type = Post, User, Group
    object_id = 22

    (Model) Post
    user = User
    content = ''
    created_at = '12082017'


    In views.py, I want to get sorted queryset like,



     Post.objects.order_by('-like')[:30] 


    Since my Post model doesn't have like field, I can't use order_by.



    I'm thinking about adding score field to track of likes and current time since then. Do you think it will work? :)



    Also any thought or advice on how to make popular feed will be highly appreciated. Thank you










    share|improve this question


























      up vote
      0
      down vote

      favorite
      1









      up vote
      0
      down vote

      favorite
      1






      1





      I want to implement 'Popular' Feed. I want to show the series of posts which has the likes the most.



      Here is abstract model schema.



      (Model) Like 
      user = User
      content_type = Post, User, Group
      object_id = 22

      (Model) Post
      user = User
      content = ''
      created_at = '12082017'


      In views.py, I want to get sorted queryset like,



       Post.objects.order_by('-like')[:30] 


      Since my Post model doesn't have like field, I can't use order_by.



      I'm thinking about adding score field to track of likes and current time since then. Do you think it will work? :)



      Also any thought or advice on how to make popular feed will be highly appreciated. Thank you










      share|improve this question















      I want to implement 'Popular' Feed. I want to show the series of posts which has the likes the most.



      Here is abstract model schema.



      (Model) Like 
      user = User
      content_type = Post, User, Group
      object_id = 22

      (Model) Post
      user = User
      content = ''
      created_at = '12082017'


      In views.py, I want to get sorted queryset like,



       Post.objects.order_by('-like')[:30] 


      Since my Post model doesn't have like field, I can't use order_by.



      I'm thinking about adding score field to track of likes and current time since then. Do you think it will work? :)



      Also any thought or advice on how to make popular feed will be highly appreciated. Thank you







      python django django-models django-rest-framework django-views






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 8 '17 at 8:25









      rajkris

      1,1351211




      1,1351211










      asked Dec 8 '17 at 3:44









      John Baek

      714841




      714841
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          You have something called the reverse generic relation for this purpose:



          Reverse Generic Relations



          Add the reverse generic relation to your post model and you can get the count of the number of likes and then order it based on likes for popularity based ordering.



          In your post model add:



          likes = GenericRelation(Like)


          Maybe use annotate for ordering:



          Post.objects.annotate(like_count=Count('likes')).order_by('-like_count')


          You would get an additional field 'like_count' for each post object plus it will be ordered based on the number of likes.






          share|improve this answer























          • THIS WORKS! :) Thank you so much. Great
            – John Baek
            Dec 13 '17 at 9:34










          • Glad it helped.
            – rajkris
            Dec 14 '17 at 4:47










          • stackoverflow.com/questions/48005427/…
            – John Baek
            Dec 28 '17 at 12:13











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f47707377%2fdjango-order-objects-based-on-likes-for-popular-feed%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          You have something called the reverse generic relation for this purpose:



          Reverse Generic Relations



          Add the reverse generic relation to your post model and you can get the count of the number of likes and then order it based on likes for popularity based ordering.



          In your post model add:



          likes = GenericRelation(Like)


          Maybe use annotate for ordering:



          Post.objects.annotate(like_count=Count('likes')).order_by('-like_count')


          You would get an additional field 'like_count' for each post object plus it will be ordered based on the number of likes.






          share|improve this answer























          • THIS WORKS! :) Thank you so much. Great
            – John Baek
            Dec 13 '17 at 9:34










          • Glad it helped.
            – rajkris
            Dec 14 '17 at 4:47










          • stackoverflow.com/questions/48005427/…
            – John Baek
            Dec 28 '17 at 12:13















          up vote
          1
          down vote



          accepted










          You have something called the reverse generic relation for this purpose:



          Reverse Generic Relations



          Add the reverse generic relation to your post model and you can get the count of the number of likes and then order it based on likes for popularity based ordering.



          In your post model add:



          likes = GenericRelation(Like)


          Maybe use annotate for ordering:



          Post.objects.annotate(like_count=Count('likes')).order_by('-like_count')


          You would get an additional field 'like_count' for each post object plus it will be ordered based on the number of likes.






          share|improve this answer























          • THIS WORKS! :) Thank you so much. Great
            – John Baek
            Dec 13 '17 at 9:34










          • Glad it helped.
            – rajkris
            Dec 14 '17 at 4:47










          • stackoverflow.com/questions/48005427/…
            – John Baek
            Dec 28 '17 at 12:13













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          You have something called the reverse generic relation for this purpose:



          Reverse Generic Relations



          Add the reverse generic relation to your post model and you can get the count of the number of likes and then order it based on likes for popularity based ordering.



          In your post model add:



          likes = GenericRelation(Like)


          Maybe use annotate for ordering:



          Post.objects.annotate(like_count=Count('likes')).order_by('-like_count')


          You would get an additional field 'like_count' for each post object plus it will be ordered based on the number of likes.






          share|improve this answer














          You have something called the reverse generic relation for this purpose:



          Reverse Generic Relations



          Add the reverse generic relation to your post model and you can get the count of the number of likes and then order it based on likes for popularity based ordering.



          In your post model add:



          likes = GenericRelation(Like)


          Maybe use annotate for ordering:



          Post.objects.annotate(like_count=Count('likes')).order_by('-like_count')


          You would get an additional field 'like_count' for each post object plus it will be ordered based on the number of likes.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 8 '17 at 4:25

























          answered Dec 8 '17 at 4:17









          rajkris

          1,1351211




          1,1351211












          • THIS WORKS! :) Thank you so much. Great
            – John Baek
            Dec 13 '17 at 9:34










          • Glad it helped.
            – rajkris
            Dec 14 '17 at 4:47










          • stackoverflow.com/questions/48005427/…
            – John Baek
            Dec 28 '17 at 12:13


















          • THIS WORKS! :) Thank you so much. Great
            – John Baek
            Dec 13 '17 at 9:34










          • Glad it helped.
            – rajkris
            Dec 14 '17 at 4:47










          • stackoverflow.com/questions/48005427/…
            – John Baek
            Dec 28 '17 at 12:13
















          THIS WORKS! :) Thank you so much. Great
          – John Baek
          Dec 13 '17 at 9:34




          THIS WORKS! :) Thank you so much. Great
          – John Baek
          Dec 13 '17 at 9:34












          Glad it helped.
          – rajkris
          Dec 14 '17 at 4:47




          Glad it helped.
          – rajkris
          Dec 14 '17 at 4:47












          stackoverflow.com/questions/48005427/…
          – John Baek
          Dec 28 '17 at 12:13




          stackoverflow.com/questions/48005427/…
          – John Baek
          Dec 28 '17 at 12:13


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f47707377%2fdjango-order-objects-based-on-likes-for-popular-feed%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Feedback on college project

          Futebolista

          Albești (Vaslui)