[Answer]-What's the "Django Way" to Do This Query?

1👍

You can do this:

first_picture_without_comment = Picture.objects.exclude(id__in= Comment.objects.filter(user= request.user).values_list('picture__id', flat=True)).order_by('id')[0]

To simplify:

pic_ids_with_comments = Comment.objects.filter(user= request.user).values_list('picture__id', flat=True)
first_picture_without_comment_qs = Picture.objects.exclude(id__in= pic_ids_with_comments).order_by('id')
if first_picture_without_comment_qs.exists(): 
    first_pic = first_picture_without_comment_qs[0]

This gives the first picture created (based on id), which is not commented by this user

Leave a comment