Skip to content

Commit

Permalink
fix annotation last_changed for nulls with coalesce
Browse files Browse the repository at this point in the history
  • Loading branch information
MyPyDavid committed Apr 11, 2023
1 parent 3f7aec2 commit 3017804
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions rdmo/projects/views/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.core.exceptions import PermissionDenied
from django.db import models
from django.db.models import F, OuterRef, Subquery
from django.db.models.functions import Coalesce, Greatest
from django.forms import Form
from django.http import Http404
from django.shortcuts import get_object_or_404, redirect
Expand Down Expand Up @@ -53,7 +54,7 @@ def get_queryset(self):
last_changed_subquery = models.Subquery(
Value.objects.filter(project=models.OuterRef('pk')).order_by('-updated').values('updated')[:1]
)
queryset = queryset.annotate(last_changed=models.functions.Greatest('updated', last_changed_subquery))
queryset = queryset.annotate(last_changed=Coalesce(Greatest(last_changed_subquery, 'updated'), 'updated'))

# order by last changed
queryset = queryset.order_by('-last_changed')
Expand Down Expand Up @@ -91,7 +92,7 @@ def get_queryset(self):
last_changed_subquery = models.Subquery(
Value.objects.filter(project=models.OuterRef('pk')).order_by('-updated').values('updated')[:1]
)
queryset = queryset.annotate(last_changed=models.functions.Greatest('updated', last_changed_subquery))
queryset = queryset.annotate(last_changed=Coalesce(Greatest(last_changed_subquery, 'updated'), 'updated'))

return queryset
else:
Expand Down

0 comments on commit 3017804

Please sign in to comment.