On Nov 5, 11:34 am, Bobo <[EMAIL PROTECTED]> wrote:
> Hi everyone,
>
> 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.
>
> 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)

This isn't a very efficient way to do this. You should be using
filter() not all() here.
messages = Message.objects.filter(recipient__in=['Alle', department])
Then you don't need the loop and the messagelist variable at all.

Having done this, you can just slice off the first two:
messages = Message.objects.filter(recipient__in=['Alle', department])[:
2]

Or, you could do it in the template:

{% for i in messagelist|slice:":2"%}

--
DR.
--~--~---------~--~----~------------~-------~--~----~
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