Re: Filter on related model problem...

2007-08-15 Thread [EMAIL PROTECTED]

Yes, you have to sort by the column you are regrouping--the regrouping
doesn't change the position of the rows.

Good example here: http://www.djangoproject.com/documentation/templates/

On Aug 14, 9:18 pm, Carl Karsten <[EMAIL PROTECTED]> wrote:
> Collin Grady wrote:
> > Regroup generates a list of dicts with two keys - grouper (the value
> > of the field you're grouping by) and list (the list of objects that
> > match that)
>
> > So in this case, you get entries like {'grouper': ,
> > 'list': [, , ...]}  :)
>
> why is it call regroup and not just group?  The original data set does not 
> need
> to be sorted/grouped, right?
>
> Carl K


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Filter on related model problem...

2007-08-14 Thread Carl Karsten

Collin Grady wrote:
> Regroup generates a list of dicts with two keys - grouper (the value
> of the field you're grouping by) and list (the list of objects that
> match that)
> 
> So in this case, you get entries like {'grouper': ,
> 'list': [, , ...]}  :)
> 
> 

why is it call regroup and not just group?  The original data set does not need 
to be sorted/grouped, right?

Carl K

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Filter on related model problem...

2007-08-14 Thread [EMAIL PROTECTED]

Thanks for all the help.

I ended up using this:

View...
s = User.objects.all().values('id', 'username', 'first_name',
'last_name',)
users = []
for u in s:
p = Poem.objects.filter(user=u['id'], approved=True)
users.append({
'id':   u['id'],
'username': u['username'],
'first_name':   u['first_name'],
'last_name':u['last_name'],
'poems':p,
})


Template...
{% if users %}

{% for user in users %}
{{ user.first_name }} {{ user.last_name }}, {{ user.poems.count }}
Poems
{% endfor %}

{% endif %}

On Aug 13, 9:49 pm, Collin Grady <[EMAIL PROTECTED]> wrote:
> Regroup generates a list of dicts with two keys - grouper (the value
> of the field you're grouping by) and list (the list of objects that
> match that)
>
> So in this case, you get entries like {'grouper': ,
> 'list': [, , ...]}  :)


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Filter on related model problem...

2007-08-13 Thread Collin Grady

Regroup generates a list of dicts with two keys - grouper (the value
of the field you're grouping by) and list (the list of objects that
match that)

So in this case, you get entries like {'grouper': ,
'list': [, , ...]}  :)


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Filter on related model problem...

2007-08-13 Thread [EMAIL PROTECTED]

Nice--I was thinking about using regroup, but I didn't think this was
a "proper" use of it. Thanks!

On Aug 13, 8:22 pm, Collin Grady <[EMAIL PROTECTED]> wrote:
> view:
>
> poems =
> Poem.objects.filter(approved=True).select_related().order_by('auth_user.use 
> rname')
>
> template:
>
> {% regroup poems by user as grouped %}
> {% for group in grouped %}
> {{ group.grouper }}
> {% for poem in group.list %}
> {{ poem.title }}
> {% endfor %}
> {% endfor %}
>
> Perhaps something like that? :)


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Filter on related model problem...

2007-08-13 Thread Carl Karsten

Collin Grady wrote:
> view:
> 
> poems =
> Poem.objects.filter(approved=True).select_related().order_by('auth_user.username')
> 
> template:
> 
> {% regroup poems by user as grouped %}
> {% for group in grouped %}
> {{ group.grouper }}
> {% for poem in group.list %}
> {{ poem.title }}
> {% endfor %}
> {% endfor %}
> 

Can you explain how regroup works?

This is probably the solution to my problem  i posted a few days ago: subject: 
grouping, replies: 0.

Carl K

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Filter on related model problem...

2007-08-13 Thread Collin Grady

view:

poems =
Poem.objects.filter(approved=True).select_related().order_by('auth_user.username')

template:

{% regroup poems by user as grouped %}
{% for group in grouped %}
{{ group.grouper }}
{% for poem in group.list %}
{{ poem.title }}
{% endfor %}
{% endfor %}

Perhaps something like that? :)


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Filter on related model problem...

2007-08-13 Thread [EMAIL PROTECTED]

> u = User.objects.filter(poem_set__approved=True)

I think you meant: u = User.objects.filter(poem__approved=True)

> and then in your template, you can have something like
>
>  {% for user in users %}
>User: {{ user }}
>{% for poem in user.poems %}
>  {% if poem.approved %}
>   {{ poem.title }}
>  {% endif %}
>{% endfor %}
>  {% endfor %}

I would like to do something like that, but I don't want to filter the
approved poems in the template. I want to filter them in the view.
(Thank for your help though!)




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Filter on related model problem...

2007-08-13 Thread Tim Chase

> I have a "poem" model that belongs to "user". The "poem" model an
> "approved" attribute. I want to print a list of users and display only
> their poems that are approved.
> 
> What do I specify in the Queryset to make this work?
> 
> I want to do something like this:
> u = User.objects.filter(poem.approved=True)

If you're interested in poems that have been approved, you want
something like

poems = Poems.objects.filter(approved=True, user=request.user)

Or perhaps you *do* want users and their approved poems in which
case you'd have something like

u = User.objects.filter(...users of interest...)

or perhaps

u = User.objects.filter(poem_set__approved=True)

and then in your template, you can have something like

 {% for user in users %}
   User: {{ user }}
   {% for poem in user.poems %}
 {% if poem.approved %}
  {{ poem.title }}
 {% endif %}
   {% endfor %}
 {% endfor %}


-tim





--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Filter on related model problem...

2007-08-13 Thread [EMAIL PROTECTED]

I have a "poem" model that belongs to "user". The "poem" model an
"approved" attribute. I want to print a list of users and display only
their poems that are approved.

What do I specify in the Queryset to make this work?

I want to do something like this:
u = User.objects.filter(poem.approved=True)

But that obviously is the incorrect syntax.

model:

class Poem(models.Model):
title = models.CharField('Title', maxlength=127)
body = models.TextField('Body Text')
approved = models.NullBooleanField()
user = models.ForeignKey(User)

Thanks in advance!


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---