I would guess that this version:
   messages = Message.objects.order_by('-created')[:2]

would be more memory efficient than the:
   messages = Message.objects.all()
   (and then using   |slice:":2" to cut out the top 2)

although, I've done that second one often.

-- joe

On Nov 5, 6:56 am, "Ronny Haryanto" <[EMAIL PROTECTED]> wrote:
> On Wed, Nov 5, 2008 at 6:34 PM, Bobo <[EMAIL PROTECTED]> wrote:
> > In my Django project I've on the front page a table where I wish to
> > load the two newest messages. Then I've a link to a message archive
> > where all messages are listed.
>
> > My problem is that I don't know how to "filter" my list at my front
> > page so it only extracts the two newest messages.
>
> messages = Message.objects.order_by('-created')[:2]
>
> Substitute "created" with the actual date field if it's different.
>
> > This is my code in views.py where I extracts all the messages and
> > append them to my list:
>
> > messages = Message.objects.all()
> > messagelist = []
> > for i in messages:
> > if i.recepient=='Alle':
> > messagelist.append(i)
> > elif i.recepient==department:
> > messagelist.append(i)
>
> You don't have to pull all() if you only need a sub
>
> from django.db.models import Q
>
> messages = Message.objects.filter(
>     Q(recepient='Alle') | Q(recepient=department)).order_by('-created')[:2]
>
> or if you don't want to use Q:
>
> messages = Message.objects.filter(
>     recepient__in=['Alle', department]).order_by('-created')[:2]
>
> > Later on my plan is to make the archive so the messages again is
> > filtered or sorted in month, so the user can see that e.g. in November
> > month this and this message was posted and in October, this and this
> > message, but that's a whole new question :-)
>
> Search for "generic views" in the documentation, you might be
> interested in the date based views.
>
> Ronny
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to