First, if you aren't running into db performance problems, I wouldn't
optimize. Keep everything as simple as possible, then optimize the
parts that actually demonstrate themselves to be a performance issue.

If this really is a performance issue, you could solve it like this:

If you know that you need the whole list why not do this logic in the
view? This should only make one db query.

mylist = list(MyModel.objects.filter(type="A")|MyModel.objects.filter
(type="B"))

mylist_type_a = [ x for x in mylist if x.type == "A" ]
mylist_type_b = [ x for x in mylist if x.type == "B" ]

Then pass both mylist_type_* variables in your context.

On Aug 7, 1:35 am, bweiss <[email protected]> wrote:
> Is there a simple way to do the following that I'm just not seeing, or
> am I looking at trying to write a custom tag?  The functionality I
> need is similar to {% regroup %} but not quite the same...
>
> My app currently has a custom admin view in which I've defined a whole
> bunch of different lists, which are all objects of the same model,
> filtered according to different options of a field called "Type".
> (eg. type_A_list = mymodel.objects.filter(Type="A"); type_B_list =
> mymodel.objects.filter(Type="B"); etc.)
>
> I've realised that the number of database hits this involves is
> inefficient, and it would be cleaner to have a single list of objects
> and try to perform the logic I need at the template level.
>
> What I'd like to be able to do is, for a single variable,
> "object_list", define subsets of this list containing objects that
> meet a certain condition.  So, to filter by the field "Type", I could
> define lists called "type_A_list", "type_B_list", etc, that could be
> iterated through in the same way as the original list.
>
> The reason I need to do this is to be able to use the {% if
> type_A_list %} tag in order to flag when there are NO objects of a
> given type.  This is why (as far as I can see) the {% regroup %} tag
> won't quite work, as it only lists the groups that actually have
> members.
>
> Output would look something like:
>
> Type 1:
> Object 1, Object 5, Object 6
>
> Type 2:
> There are no objects of Type 2
>
> Type 3:
> Object 2, Object 4
>
> Does anyone have any suggestions?
>
> Thanks,
> Bianca
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to