Skip to content

Instantly share code, notes, and snippets.

@walison17
Created August 16, 2019 15:57
Show Gist options
  • Save walison17/e12ea627dcc1516e84b08a3aef9d9d3c to your computer and use it in GitHub Desktop.
Save walison17/e12ea627dcc1516e84b08a3aef9d9d3c to your computer and use it in GitHub Desktop.
courses = Course.objects.filter(published_at__lte=timezone.now())
lessons_with_status = Lesson.objects.annotate(
is_completed=models.Case(
models.When(
models.Q(student_progress__completed_at__isnull=False)
& models.Q(student_progress__user=user),
then=models.Value(True)
),
default=models.Value(False),
output_field=models.BooleanField()
)
)
courses.filter(user_course__user=user).prefetch_related(
models.Prefetch('modules__lessons', queryset=lessons_with_status)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment