Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » annotate with query set for distinct values [RSS Feed]

#1 June 11, 2010 00:11:37

S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

annotate with query set for distinct values


Thanks.

I've already tried that but this isn't quite what i'm looking for. As
u wrote this returns a list, but I would like to obtain my calculated
property when I'm iterating over this. Like so (let's say the result
is in res variable) :

{% for r in res %}
{{r.xxx}} - {{r.name__count}}
{% endfor %}

I'm starting to think that there's no such nice way to acheive that :/

On 10 Cze, 23:06, Dan Harris <dih0...@gmail.com> wrote:
> Here is an example:
>
> # In models.py
> class TestModel(models.Model):
>    name = models.CharField()
>
> # Get a listing of unique names and their counts
> # In some view
> from django.db.models import Count
>
> TestModel.objects.values("name").annotate(Count("name"))
>
> This will return a list of the form:
>
>
>
> Hope this helps!
>
> Dan Harris
> dih0...@gmail.com
>
> On Jun 10, 5:01 pm, SlafS <slaf...@gmail.com> wrote:
>
> > Hi there!
> > I have a question. If i have a model with some fields (let's say all
> > CharFields named aaa,bbb,ccc etc. and some calculated properties named
> > xxx and zzz) how can I obtain something similar to
>
> > "SELECT aaa, count(aaa) FROM my_model_table GROUP BY aaa;"
> > i.e. a list of - lazy loaded - objects with distinct aaa values and an
> > extra column which indicates how many entries of a specific aaa are in
> > the table.
>
> > I would like to acheive that with QuerySet and annotate so that I can
> > get a list of objects to use their other properties (xxx or zzz) e.g.
> > in my template
>
> > Regards

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 June 11, 2010 00:17:13

Dan H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

annotate with query set for distinct values


The problem is the GROUP BY you are looking to get. The problem with
doing a group by you don't get the whole object which is what the
Django queryset is trying to return:

res = queryset.annotate(Count("name"))

This will give you a result set that you want and you can iterate over
like:

{% for r in res %}
{{ r.name }} - {{ r.name__count}}
{% endfor %}

however there may be duplicated {{ r.name }}

Dan Harris
dih0...@gmail.com

On Jun 10, 5:24 pm, SlafS <slaf...@gmail.com> wrote:
> Thanks.
>
> I've already tried that but this isn't quite what i'm looking for. As
> u wrote this returns a list, but I would like to obtain my calculated
> property when I'm iterating over this. Like so (let's say the result
> is in res variable) :
>
> {% for r in res %}
>    {{r.xxx}} - {{r.name__count}}
> {% endfor %}
>
> I'm starting to think that there's no such nice way to acheive that :/
>
> On 10 Cze, 23:06, Dan Harris <dih0...@gmail.com> wrote:
>
>
>
> > Here is an example:
>
> > # In models.py
> > class TestModel(models.Model):
> >    name = models.CharField()
>
> > # Get a listing of unique names and their counts
> > # In some view
> > from django.db.models import Count
>
> > TestModel.objects.values("name").annotate(Count("name"))
>
> > This will return a list of the form:
>
> >
>
> > Hope this helps!
>
> > Dan Harris
> > dih0...@gmail.com
>
> > On Jun 10, 5:01 pm, SlafS <slaf...@gmail.com> wrote:
>
> > > Hi there!
> > > I have a question. If i have a model with some fields (let's say all
> > > CharFields named aaa,bbb,ccc etc. and some calculated properties named
> > > xxx and zzz) how can I obtain something similar to
>
> > > "SELECT aaa, count(aaa) FROM my_model_table GROUP BY aaa;"
> > > i.e. a list of - lazy loaded - objects with distinct aaa values and an
> > > extra column which indicates how many entries of a specific aaa are in
> > > the table.
>
> > > I would like to acheive that with QuerySet and annotate so that I can
> > > get a list of objects to use their other properties (xxx or zzz) e.g.
> > > in my template
>
> > > Regards

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 June 11, 2010 00:19:02

Dan H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

annotate with query set for distinct values


Here is an example:

# In models.py
class TestModel(models.Model):
name = models.CharField()


# Get a listing of unique names and their counts
# In some view
from django.db.models import Count

TestModel.objects.values("name").annotate(Count("name"))

This will return a list of the form:



Hope this helps!

Dan Harris
dih0...@gmail.com

On Jun 10, 5:01 pm, SlafS <slaf...@gmail.com> wrote:
> Hi there!
> I have a question. If i have a model with some fields (let's say all
> CharFields named aaa,bbb,ccc etc. and some calculated properties named
> xxx and zzz) how can I obtain something similar to
>
> "SELECT aaa, count(aaa) FROM my_model_table GROUP BY aaa;"
> i.e. a list of - lazy loaded - objects with distinct aaa values and an
> extra column which indicates how many entries of a specific aaa are in
> the table.
>
> I would like to acheive that with QuerySet and annotate so that I can
> get a list of objects to use their other properties (xxx or zzz) e.g.
> in my template
>
> Regards

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 June 11, 2010 00:20:00

S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

annotate with query set for distinct values


Yes, I'm aware of the background of the problem but i was wondering if
I can somehow "workaround" it.

Thanks for Your help

On 10 Cze, 23:34, Dan Harris <dih0...@gmail.com> wrote:
> The problem is the GROUP BY you are looking to get. The problem with
> doing a group by you don't get the whole object which is what the
> Django queryset is trying to return:
>
> res = queryset.annotate(Count("name"))
>
> This will give you a result set that you want and you can iterate over
> like:
>
> {% for r in res %}
>    {{ r.name }} - {{ r.name__count}}
> {% endfor %}
>
> however there may be duplicated {{ r.name }}
>
> Dan Harris
> dih0...@gmail.com
>
> On Jun 10, 5:24 pm, SlafS <slaf...@gmail.com> wrote:
>
> > Thanks.
>
> > I've already tried that but this isn't quite what i'm looking for. As
> > u wrote this returns a list, but I would like to obtain my calculated
> > property when I'm iterating over this. Like so (let's say the result
> > is in res variable) :
>
> > {% for r in res %}
> >    {{r.xxx}} - {{r.name__count}}
> > {% endfor %}
>
> > I'm starting to think that there's no such nice way to acheive that :/
>
> > On 10 Cze, 23:06, Dan Harris <dih0...@gmail.com> wrote:
>
> > > Here is an example:
>
> > > # In models.py
> > > class TestModel(models.Model):
> > >    name = models.CharField()
>
> > > # Get a listing of unique names and their counts
> > > # In some view
> > > from django.db.models import Count
>
> > > TestModel.objects.values("name").annotate(Count("name"))
>
> > > This will return a list of the form:
>
> > >
>
> > > Hope this helps!
>
> > > Dan Harris
> > > dih0...@gmail.com
>
> > > On Jun 10, 5:01 pm, SlafS <slaf...@gmail.com> wrote:
>
> > > > Hi there!
> > > > I have a question. If i have a model with some fields (let's say all
> > > > CharFields named aaa,bbb,ccc etc. and some calculated properties named
> > > > xxx and zzz) how can I obtain something similar to
>
> > > > "SELECT aaa, count(aaa) FROM my_model_table GROUP BY aaa;"
> > > > i.e. a list of - lazy loaded - objects with distinct aaa values and an
> > > > extra column which indicates how many entries of a specific aaa are in
> > > > the table.
>
> > > > I would like to acheive that with QuerySet and annotate so that I can
> > > > get a list of objects to use their other properties (xxx or zzz) e.g.
> > > > in my template
>
> > > > Regards

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#5 June 11, 2010 00:29:46

sebastien p.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

annotate with query set for distinct values


I think you didn't really look well anotate, because it looks like it
is what you qre looking for ! Or is there something I misunderstood ?http://docs.djangoproject.com/en/dev/ref/models/querysets/#annotate-args-kwargs"Annotates each object in the QuerySet with the provided list of
aggregate values (averages, sums, etc) that have been computed over
the objects that are related to the objects in the QuerySet. Each
argument to annotate() is an annotation that will be added to each
object in the QuerySet that is returned."

On Jun 11, 12:24 am, SlafS <slaf...@gmail.com> wrote:
> Thanks.
>
> I've already tried that but this isn't quite what i'm looking for. As
> u wrote this returns a list, but I would like to obtain my calculated
> property when I'm iterating over this. Like so (let's say the result
> is in res variable) :
>
> {% for r in res %}
>    {{r.xxx}} - {{r.name__count}}
> {% endfor %}
>
> I'm starting to think that there's no such nice way to acheive that :/
>
> On 10 Cze, 23:06, Dan Harris <dih0...@gmail.com> wrote:
>
>
>
> > Here is an example:
>
> > # In models.py
> > class TestModel(models.Model):
> >    name = models.CharField()
>
> > # Get a listing of unique names and their counts
> > # In some view
> > from django.db.models import Count
>
> > TestModel.objects.values("name").annotate(Count("name"))
>
> > This will return a list of the form:
>
> >
>
> > Hope this helps!
>
> > Dan Harris
> > dih0...@gmail.com
>
> > On Jun 10, 5:01 pm, SlafS <slaf...@gmail.com> wrote:
>
> > > Hi there!
> > > I have a question. If i have a model with some fields (let's say all
> > > CharFields named aaa,bbb,ccc etc. and some calculated properties named
> > > xxx and zzz) how can I obtain something similar to
>
> > > "SELECT aaa, count(aaa) FROM my_model_table GROUP BY aaa;"
> > > i.e. a list of - lazy loaded - objects with distinct aaa values and an
> > > extra column which indicates how many entries of a specific aaa are in
> > > the table.
>
> > > I would like to acheive that with QuerySet and annotate so that I can
> > > get a list of objects to use their other properties (xxx or zzz) e.g.
> > > in my template
>
> > > Regards

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#6 June 11, 2010 12:48:45

Scott G.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

annotate with query set for distinct values


It's not really a matter of "working around" it. Your .xxx method/
property is an attribute of the object. What you evidently want from
the database is *not* a list of those objects, but rather a summary
representation of them. Trying to apply your .xxx is meaningless as
you don't have a discrete object to apply it to. If it *is* supposed
to be meaningful in that context, then it's probably the sort of thing
that belongs in a manager.

If you can tell us a bit more about what .xxx actually is, or is
supposed to do, then we might be able to come up with an alternate
solution.

Regards
Scott


On Jun 10, 5:42 pm, SlafS <slaf...@gmail.com> wrote:
> Yes, I'm aware of the background of the problem but i was wondering if
> I can somehow "workaround" it.
>
> Thanks for Your help
>
> On 10 Cze, 23:34, Dan Harris <dih0...@gmail.com> wrote:
>
>
>
> > The problem is the GROUP BY you are looking to get. The problem with
> > doing a group by you don't get the whole object which is what the
> > Django queryset is trying to return:
>
> > res = queryset.annotate(Count("name"))
>
> > This will give you a result set that you want and you can iterate over
> > like:
>
> > {% for r in res %}
> >    {{ r.name }} - {{ r.name__count}}
> > {% endfor %}
>
> > however there may be duplicated {{ r.name }}
>
> > Dan Harris
> > dih0...@gmail.com
>
> > On Jun 10, 5:24 pm, SlafS <slaf...@gmail.com> wrote:
>
> > > Thanks.
>
> > > I've already tried that but this isn't quite what i'm looking for. As
> > > u wrote this returns a list, but I would like to obtain my calculated
> > > property when I'm iterating over this. Like so (let's say the result
> > > is in res variable) :
>
> > > {% for r in res %}
> > >    {{r.xxx}} - {{r.name__count}}
> > > {% endfor %}
>
> > > I'm starting to think that there's no such nice way to acheive that :/
>
> > > On 10 Cze, 23:06, Dan Harris <dih0...@gmail.com> wrote:
>
> > > > Here is an example:
>
> > > > # In models.py
> > > > class TestModel(models.Model):
> > > >    name = models.CharField()
>
> > > > # Get a listing of unique names and their counts
> > > > # In some view
> > > > from django.db.models import Count
>
> > > > TestModel.objects.values("name").annotate(Count("name"))
>
> > > > This will return a list of the form:
>
> > > >
>
> > > > Hope this helps!
>
> > > > Dan Harris
> > > > dih0...@gmail.com
>
> > > > On Jun 10, 5:01 pm, SlafS <slaf...@gmail.com> wrote:
>
> > > > > Hi there!
> > > > > I have a question. If i have a model with some fields (let's say all
> > > > > CharFields named aaa,bbb,ccc etc. and some calculated properties named
> > > > > xxx and zzz) how can I obtain something similar to
>
> > > > > "SELECT aaa, count(aaa) FROM my_model_table GROUP BY aaa;"
> > > > > i.e. a list of - lazy loaded - objects with distinct aaa values and an
> > > > > extra column which indicates how many entries of a specific aaa are in
> > > > > the table.
>
> > > > > I would like to acheive that with QuerySet and annotate so that I can
> > > > > get a list of objects to use their other properties (xxx or zzz) e.g.
> > > > > in my template
>
> > > > > Regards

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » annotate with query set for distinct values [RSS Feed]

Board footer

Moderator control

Enjoy the 16th of December
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support