Can You please help me out!!!

24 views
Skip to first unread message

meera gangani

unread,
May 29, 2020, 7:00:19 AM5/29/20
to django...@googlegroups.com
i want to fetch last 3 records for notification!!
But I only fetch last 1 record.

Can you please help me out!!!
Can i use filters??

And my model fields are:
title=
message=
date=

And i want to fetch last 3-4 records
Screen Shot 2020-05-29 at 4.25.07 PM.png
Screen Shot 2020-05-29 at 4.25.41 PM.png
Screen Shot 2020-05-29 at 4.25.25 PM.png

Sencer Hamarat

unread,
May 29, 2020, 7:39:09 AM5/29/20
to django...@googlegroups.com
Hi Meera,

You have only one record because of you are using .latest() method in the query.

If you want to last 3 or 4 object ordered by descending date, you should write queryset like this:

notifications = Notification.objects.all().order_by('-date')[0:x]  # The 'x' could be 3 or 4 by your choice.

An pass the notifications to request context in 'render_to_response' call. {'notifications': notifications}

The you have to iterate notifications in template via for loop. For example;

{% for notification in notifications %}
<div>notification.title</div>
<div>notification.message</div>
<div>notification.date</div>
{% endfor %}

Saygılarımla,
Sencer HAMARAT



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CANaPPPJVprm5gE0FNFWqPDGdW_ciq15JQXLmjVHinJkHPwRdqQ%40mail.gmail.com.

meera gangani

unread,
May 29, 2020, 11:18:04 PM5/29/20
to django...@googlegroups.com
Thank you So much for helping me out!!
Thanks a lot

Meera Gangani

meera gangani

unread,
May 29, 2020, 11:29:00 PM5/29/20
to django...@googlegroups.com
I Want to implement Mark all a read in notification!!
So What should i do!!
Can you explain me a little bit of this!

Julio Cojom

unread,
Jun 1, 2020, 12:48:08 AM6/1/20
to django...@googlegroups.com
Boolean field on notification model that change status when click or view the notification , this perhaps need an asynchronous task with jQuery or celery

Rewards 

Reply all
Reply to author
Forward
0 new messages