Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get model likes count? #63

Open
selcuko opened this issue Aug 23, 2020 · 2 comments
Open

How to get model likes count? #63

selcuko opened this issue Aug 23, 2020 · 2 comments
Labels

Comments

@selcuko
Copy link

selcuko commented Aug 23, 2020

Hello devs.
I am proudly using your package, but I have an issue.
I'd like to get my model's total likes programmatically, will be using this to list the entries with most likes.
Thank you.

@selcuko
Copy link
Author

selcuko commented Aug 23, 2020

Hey. Here are some of my progress.
I runned graphviz to visualize relations within models.
my_project_visualized
There's no obvious link between Like and Entry.
When I double tabbed to see all attributes of a Like, I saw "receiver" which is a Entry. So I can see a Like's receiver, but I still cannot see an Entries likes. I am a bit confused.

@selcuko
Copy link
Author

selcuko commented Aug 24, 2020

Okay. I have been inspecting your source code and came up with a solution.

@register.simple_tag
def who_likes(obj):
    """
    Usage:
        {% who_likes obj as var %}
    """
    return Like.objects.filter(
        receiver_content_type=ContentType.objects.get_for_model(obj),
        receiver_object_id=obj.pk
    )

with this code, I was able to get a model's like count within templates, now going to implement it for programmatical use.
but I still cannot think of a way to effectively get the model with most likes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant