Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Nov. 15, 2005 08:15:33

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

understanding regroup


I have the following classes in my model:

class Category:
description = charField()

class Thing:
self.description = charField()
self.category(ForeignKey, Category)

I would like a page layed out like:

Category1
* foo
* bar
* baz

CategoryX
* woo
* war
* waz

etc.

The {% regroup %} tag looked promising, but I can not get it to work.

{% regroup user.get_thing_list|dictsort:"category" by category as
grouped %\
}
{% for category in grouped %}
<p>
{{ category.grouper }}
{% for entry in category.list %}
<!-- handle entry -->
{% endfor %}
</p>
{% endfor %}

Offline

#2 Nov. 15, 2005 14:46:36

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

understanding regroup


On Nov 15, 2005, at 2:15 AM, Sean Perry wrote:The {% regroup %} tag looked promising, but I can not get it to work.{% regroup user.get_thing_list|dictsort:"category" by categoryas grouped %}You have to group by actual fields (and not relationship names), soto Things by Category you would need to do something like::{% regroup thing_list|dictsort:"category_id" by category_id asgrouped %}(Of course, if you wanted to make sure your list was in order sortedby the *title* of your category, which would certainly look nicer,you probably would want::{% regroup thing_list|dictsort:"description" by description asgrouped %})

Clear as mud?

Jacob

Offline

#3 Nov. 15, 2005 17:08:33

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

understanding regroup


Jacob Kaplan-Moss wrote:On Nov 15, 2005, at 2:15 AM, Sean Perry wrote:The {% regroup %} tag looked promising, but I can not get it to work.{% regroup user.get_thing_list|dictsort:"category" by category asgrouped %}You have to group by actual fields (and not relationship names), so toThings by Category you would need to do something like::{% regroup thing_list|dictsort:"category_id" by category_id asgrouped %}(Of course, if you wanted to make sure your list was in order sorted bythe *title* of your category, which would certainly look nicer, youprobably would want::{% regroup thing_list|dictsort:"description" by description asgrouped %})

Clear as mud?This much I kinda figured out last night. I don't even mind sorting bycategory_id if there was a way to then transform id -> description later on.My gut feel is I have to fall back to populating the list in the viewcode where I have the full Python API. Which is kinda lame. Means mytemplates are pretty much useless for many of my planned projects. Feelswrong to have to walk back up to the heavy code for something that seemsto be a common need.

Offline

#4 Nov. 15, 2005 17:30:18

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

understanding regroup


Sean Perry wrote:
>
> I have the following classes in my model:
>
> class Category:
> description = charField()
>
> class Thing:
> self.description = charField()
> self.category(ForeignKey, Category)
>
> I would like a page layed out like:
>
> Category1
> * foo
> * bar
> * baz
>
> CategoryX
> * woo
> * war
> * waz
>
> etc.
>
> The {% regroup %} tag looked promising, but I can not get it to work.
>
> {% regroup user.get_thing_list|dictsort:"category" by category as
> grouped %\
> }
> {% for category in grouped %}
> <p>
> {{ category.grouper }}
> {% for entry in category.list %}
> <!-- handle entry -->
> {% endfor %}
> </p>
> {% endfor %}
>
>
Would:

{% regroup user.get_thing_list|dictsort:"category_id" by category_id as
grouped %}
{% for category in grouped %}
<p>
{{ category.list.0.get_category.description }}
{% for entry in category.list %}
<!-- handle entry -->
{% endfor %}
</p>
{% endfor %}

work?

Offline

#5 Nov. 15, 2005 17:35:02

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

understanding regroup


Robert Wittams wrote:Would:

{% regroup user.get_thing_list|dictsort:"category_id" by category_id as
grouped %}
{% for category in grouped %}
<p>
{{ category.list.0.get_category.description }}
{% for entry in category.list %}
<!-- handle entry -->
{% endfor %}
</p>
{% endfor %}

work?looks promising, I had no idea that I could do that. Seems a bit heavyhanded. Will play around it with it some more, see how I feel about it.I also plan to try adding code to the view, see how that works out too.Maybe the answer is I end up coding some custom template stuff andsending in some patches. Or maybe there is another way hiding under adifferent rock.

Offline

Board footer

Moderator control

Enjoy the 18th of November
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