Home > Mobile >  daisychaining select_related functions?
daisychaining select_related functions?

Time:12-06

I have the below which joins the notifications model to the task model and returns all fields - it works!

notificationsx = notifications.objects.filter(orgid__in=t).select_related('task').all()

But, I also need to join the task model to the projects model - so I have all task, notifications and project fields in one output.I tried the below but it didn't work daisy chaining all the things together.

Can anyone help me please?

notificationsx = notifications.objects.filter(orgid__in=t).select_related('task').all().select_related('project').all().order_by('-epoch')[:15]

CodePudding user response:

Just put all of them in a single .select_related(…) clause [Django-doc]:

notificationsx = (
    notifications.objects.filter(orgid__in=t)
    .select_related('task', 'project')
    .order_by('-epoch')[:15]
)
  • Related