Django: Order objects based on likes for popular feed
up vote
0
down vote
favorite
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
add a comment |
up vote
0
down vote
favorite
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
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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
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
python django django-models django-rest-framework django-views
edited Dec 8 '17 at 8:25
rajkris
1,1351211
1,1351211
asked Dec 8 '17 at 3:44
John Baek
714841
714841
add a comment |
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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