1👍
✅
In the context you can pass the IndividualTask
s with the TaskUpdate
s ordered in descending order with a Prefetch
object [Django-doc]:
from django.db.models import Prefetch
context = {
'tasks' : IndividualTask.objects.prefetch_related(
Prefetch('taskupdate_set', TaskUpdate.objects.order_by('-update_created', '-update_time_created'))
)
}
then in the template you enumerate over the taskupdate_set
:
{% for task in tasks %} {% for update in task.taskupdate_set.all|slice:":3" %} {{ update.update }} {% endfor %} {%endfor %}
Note: It is normally better to make use of the
settings.AUTH_USER_MODEL
[Django-doc] to refer to the user model, than to use theUser
model [Django-doc] directly. For more information you can see the referencing theUser
model section of the documentation.
Source:stackexchange.com