Django ORM : How to use Counter with annotate
tltr
Considering a list of emails:
list = ['a@a.com', 'b@a.com', 'b@a.com', 'a@a.com', 'c@a.com', 'a@a.com']
Considering a queryset of Users using these emails, how can I use annotate to count the occurrences of the Users emails in this list?
If I try this:
users = User.objects.all()
users.values('email').annotate(email_in_list=Count('email))
the result is 1
for every users.
I expect these results for each user:
a@a.com - 3
b@a.com - 2
c@a.com - 1
Initial Question
I want to see how many refered users does have a user:
refered_user = Profile.objects.filter(user__groups__name="Refered")
list = refered_user.values_list('referer_email')
# Getting all the referers who actually had refered users
referer_users = Profile.objects.filter(user__groups__name="Referer", user__email__in=list)
Now how can I see how many refered users has a referer?
I have tried this without success:
counter = Counter(refered_user)
referer_users.values('user__email').annotate(refered_num=counter['user__email'])
As requested, here are the models :
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
referer_email = models.CharField(verbose_name='Email Referer', max_length=99, blank=True)
EDIT : Considering Count()
I have tried this:
referer_users.values('user__email').annotate(refered_num=count('user__email'))
But the value of refered_num
is always equal to 1, which is logical, because in the refered_num
query, we have only User objects with one email for each.
How can I count the occurrences of the referer email in the refered_user
list and annotate it in my referer_users
query?
python django django-orm
add a comment |
tltr
Considering a list of emails:
list = ['a@a.com', 'b@a.com', 'b@a.com', 'a@a.com', 'c@a.com', 'a@a.com']
Considering a queryset of Users using these emails, how can I use annotate to count the occurrences of the Users emails in this list?
If I try this:
users = User.objects.all()
users.values('email').annotate(email_in_list=Count('email))
the result is 1
for every users.
I expect these results for each user:
a@a.com - 3
b@a.com - 2
c@a.com - 1
Initial Question
I want to see how many refered users does have a user:
refered_user = Profile.objects.filter(user__groups__name="Refered")
list = refered_user.values_list('referer_email')
# Getting all the referers who actually had refered users
referer_users = Profile.objects.filter(user__groups__name="Referer", user__email__in=list)
Now how can I see how many refered users has a referer?
I have tried this without success:
counter = Counter(refered_user)
referer_users.values('user__email').annotate(refered_num=counter['user__email'])
As requested, here are the models :
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
referer_email = models.CharField(verbose_name='Email Referer', max_length=99, blank=True)
EDIT : Considering Count()
I have tried this:
referer_users.values('user__email').annotate(refered_num=count('user__email'))
But the value of refered_num
is always equal to 1, which is logical, because in the refered_num
query, we have only User objects with one email for each.
How can I count the occurrences of the referer email in the refered_user
list and annotate it in my referer_users
query?
python django django-orm
can you please share your models?
– ruddra
Nov 15 '18 at 19:47
I have edited my question including the model
– Aurélien
Nov 15 '18 at 19:58
add a comment |
tltr
Considering a list of emails:
list = ['a@a.com', 'b@a.com', 'b@a.com', 'a@a.com', 'c@a.com', 'a@a.com']
Considering a queryset of Users using these emails, how can I use annotate to count the occurrences of the Users emails in this list?
If I try this:
users = User.objects.all()
users.values('email').annotate(email_in_list=Count('email))
the result is 1
for every users.
I expect these results for each user:
a@a.com - 3
b@a.com - 2
c@a.com - 1
Initial Question
I want to see how many refered users does have a user:
refered_user = Profile.objects.filter(user__groups__name="Refered")
list = refered_user.values_list('referer_email')
# Getting all the referers who actually had refered users
referer_users = Profile.objects.filter(user__groups__name="Referer", user__email__in=list)
Now how can I see how many refered users has a referer?
I have tried this without success:
counter = Counter(refered_user)
referer_users.values('user__email').annotate(refered_num=counter['user__email'])
As requested, here are the models :
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
referer_email = models.CharField(verbose_name='Email Referer', max_length=99, blank=True)
EDIT : Considering Count()
I have tried this:
referer_users.values('user__email').annotate(refered_num=count('user__email'))
But the value of refered_num
is always equal to 1, which is logical, because in the refered_num
query, we have only User objects with one email for each.
How can I count the occurrences of the referer email in the refered_user
list and annotate it in my referer_users
query?
python django django-orm
tltr
Considering a list of emails:
list = ['a@a.com', 'b@a.com', 'b@a.com', 'a@a.com', 'c@a.com', 'a@a.com']
Considering a queryset of Users using these emails, how can I use annotate to count the occurrences of the Users emails in this list?
If I try this:
users = User.objects.all()
users.values('email').annotate(email_in_list=Count('email))
the result is 1
for every users.
I expect these results for each user:
a@a.com - 3
b@a.com - 2
c@a.com - 1
Initial Question
I want to see how many refered users does have a user:
refered_user = Profile.objects.filter(user__groups__name="Refered")
list = refered_user.values_list('referer_email')
# Getting all the referers who actually had refered users
referer_users = Profile.objects.filter(user__groups__name="Referer", user__email__in=list)
Now how can I see how many refered users has a referer?
I have tried this without success:
counter = Counter(refered_user)
referer_users.values('user__email').annotate(refered_num=counter['user__email'])
As requested, here are the models :
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
referer_email = models.CharField(verbose_name='Email Referer', max_length=99, blank=True)
EDIT : Considering Count()
I have tried this:
referer_users.values('user__email').annotate(refered_num=count('user__email'))
But the value of refered_num
is always equal to 1, which is logical, because in the refered_num
query, we have only User objects with one email for each.
How can I count the occurrences of the referer email in the refered_user
list and annotate it in my referer_users
query?
python django django-orm
python django django-orm
edited Nov 16 '18 at 16:19
Aurélien
asked Nov 15 '18 at 19:45
AurélienAurélien
19713
19713
can you please share your models?
– ruddra
Nov 15 '18 at 19:47
I have edited my question including the model
– Aurélien
Nov 15 '18 at 19:58
add a comment |
can you please share your models?
– ruddra
Nov 15 '18 at 19:47
I have edited my question including the model
– Aurélien
Nov 15 '18 at 19:58
can you please share your models?
– ruddra
Nov 15 '18 at 19:47
can you please share your models?
– ruddra
Nov 15 '18 at 19:47
I have edited my question including the model
– Aurélien
Nov 15 '18 at 19:58
I have edited my question including the model
– Aurélien
Nov 15 '18 at 19:58
add a comment |
2 Answers
2
active
oldest
votes
Well, you wouldn't do it with Counter, that's a Python class for counting distinct items in an existing Python collection. You need to use the Django Count
function:
from django.db.models import Count
referer_users.values('email').annotate(refered_num=Count('email'))
Thanks for your answer. I have tried this on the first place, but refered_num was equal to 1 for every referer_users. Maybe should try this one more time.
– Aurélien
Nov 15 '18 at 20:01
Unfortunatly, it does not work. I have edited my question accordingly.
– Aurélien
Nov 16 '18 at 8:30
Hello Daniel, I tried to reformulate my problem. I think I was not clear enough.
– Aurélien
Nov 16 '18 at 16:20
add a comment |
from django.db.models import Count
referer_users = referer_users.order_by()
referer_users = referer_users.values('email')
referer_users = referer_users.annotate(refered_num=Count('email'))
I think you are adding order_by('id')
, and that's why it is giving 1. When order is added it also added to group by
fields.
I am not usingorder_by()
– Aurélien
Nov 16 '18 at 9:42
you may add default ordering in meta class
– Tolqinbek Isoqov
Nov 16 '18 at 9:47
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
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%2f53326873%2fdjango-orm-how-to-use-counter-with-annotate%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Well, you wouldn't do it with Counter, that's a Python class for counting distinct items in an existing Python collection. You need to use the Django Count
function:
from django.db.models import Count
referer_users.values('email').annotate(refered_num=Count('email'))
Thanks for your answer. I have tried this on the first place, but refered_num was equal to 1 for every referer_users. Maybe should try this one more time.
– Aurélien
Nov 15 '18 at 20:01
Unfortunatly, it does not work. I have edited my question accordingly.
– Aurélien
Nov 16 '18 at 8:30
Hello Daniel, I tried to reformulate my problem. I think I was not clear enough.
– Aurélien
Nov 16 '18 at 16:20
add a comment |
Well, you wouldn't do it with Counter, that's a Python class for counting distinct items in an existing Python collection. You need to use the Django Count
function:
from django.db.models import Count
referer_users.values('email').annotate(refered_num=Count('email'))
Thanks for your answer. I have tried this on the first place, but refered_num was equal to 1 for every referer_users. Maybe should try this one more time.
– Aurélien
Nov 15 '18 at 20:01
Unfortunatly, it does not work. I have edited my question accordingly.
– Aurélien
Nov 16 '18 at 8:30
Hello Daniel, I tried to reformulate my problem. I think I was not clear enough.
– Aurélien
Nov 16 '18 at 16:20
add a comment |
Well, you wouldn't do it with Counter, that's a Python class for counting distinct items in an existing Python collection. You need to use the Django Count
function:
from django.db.models import Count
referer_users.values('email').annotate(refered_num=Count('email'))
Well, you wouldn't do it with Counter, that's a Python class for counting distinct items in an existing Python collection. You need to use the Django Count
function:
from django.db.models import Count
referer_users.values('email').annotate(refered_num=Count('email'))
answered Nov 15 '18 at 19:52
Daniel RosemanDaniel Roseman
448k41581636
448k41581636
Thanks for your answer. I have tried this on the first place, but refered_num was equal to 1 for every referer_users. Maybe should try this one more time.
– Aurélien
Nov 15 '18 at 20:01
Unfortunatly, it does not work. I have edited my question accordingly.
– Aurélien
Nov 16 '18 at 8:30
Hello Daniel, I tried to reformulate my problem. I think I was not clear enough.
– Aurélien
Nov 16 '18 at 16:20
add a comment |
Thanks for your answer. I have tried this on the first place, but refered_num was equal to 1 for every referer_users. Maybe should try this one more time.
– Aurélien
Nov 15 '18 at 20:01
Unfortunatly, it does not work. I have edited my question accordingly.
– Aurélien
Nov 16 '18 at 8:30
Hello Daniel, I tried to reformulate my problem. I think I was not clear enough.
– Aurélien
Nov 16 '18 at 16:20
Thanks for your answer. I have tried this on the first place, but refered_num was equal to 1 for every referer_users. Maybe should try this one more time.
– Aurélien
Nov 15 '18 at 20:01
Thanks for your answer. I have tried this on the first place, but refered_num was equal to 1 for every referer_users. Maybe should try this one more time.
– Aurélien
Nov 15 '18 at 20:01
Unfortunatly, it does not work. I have edited my question accordingly.
– Aurélien
Nov 16 '18 at 8:30
Unfortunatly, it does not work. I have edited my question accordingly.
– Aurélien
Nov 16 '18 at 8:30
Hello Daniel, I tried to reformulate my problem. I think I was not clear enough.
– Aurélien
Nov 16 '18 at 16:20
Hello Daniel, I tried to reformulate my problem. I think I was not clear enough.
– Aurélien
Nov 16 '18 at 16:20
add a comment |
from django.db.models import Count
referer_users = referer_users.order_by()
referer_users = referer_users.values('email')
referer_users = referer_users.annotate(refered_num=Count('email'))
I think you are adding order_by('id')
, and that's why it is giving 1. When order is added it also added to group by
fields.
I am not usingorder_by()
– Aurélien
Nov 16 '18 at 9:42
you may add default ordering in meta class
– Tolqinbek Isoqov
Nov 16 '18 at 9:47
add a comment |
from django.db.models import Count
referer_users = referer_users.order_by()
referer_users = referer_users.values('email')
referer_users = referer_users.annotate(refered_num=Count('email'))
I think you are adding order_by('id')
, and that's why it is giving 1. When order is added it also added to group by
fields.
I am not usingorder_by()
– Aurélien
Nov 16 '18 at 9:42
you may add default ordering in meta class
– Tolqinbek Isoqov
Nov 16 '18 at 9:47
add a comment |
from django.db.models import Count
referer_users = referer_users.order_by()
referer_users = referer_users.values('email')
referer_users = referer_users.annotate(refered_num=Count('email'))
I think you are adding order_by('id')
, and that's why it is giving 1. When order is added it also added to group by
fields.
from django.db.models import Count
referer_users = referer_users.order_by()
referer_users = referer_users.values('email')
referer_users = referer_users.annotate(refered_num=Count('email'))
I think you are adding order_by('id')
, and that's why it is giving 1. When order is added it also added to group by
fields.
answered Nov 16 '18 at 9:33
Tolqinbek IsoqovTolqinbek Isoqov
696
696
I am not usingorder_by()
– Aurélien
Nov 16 '18 at 9:42
you may add default ordering in meta class
– Tolqinbek Isoqov
Nov 16 '18 at 9:47
add a comment |
I am not usingorder_by()
– Aurélien
Nov 16 '18 at 9:42
you may add default ordering in meta class
– Tolqinbek Isoqov
Nov 16 '18 at 9:47
I am not using
order_by()
– Aurélien
Nov 16 '18 at 9:42
I am not using
order_by()
– Aurélien
Nov 16 '18 at 9:42
you may add default ordering in meta class
– Tolqinbek Isoqov
Nov 16 '18 at 9:47
you may add default ordering in meta class
– Tolqinbek Isoqov
Nov 16 '18 at 9:47
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.
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%2f53326873%2fdjango-orm-how-to-use-counter-with-annotate%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
can you please share your models?
– ruddra
Nov 15 '18 at 19:47
I have edited my question including the model
– Aurélien
Nov 15 '18 at 19:58