Does Django queryset values_list return a list object?

后端 未结 1 1987
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-03 19:09

I have a Django app where users post photos, and other leave comments under the photos.

When a comment is left, I need to notify:

  1. Everyone else who wr
1条回答
  •  生来不讨喜
    2021-01-03 19:47

    The values_list method returns a ValuesListQuerySet. This means it has the advantages of a queryset. For example it is lazy, so you only fetch the first 25 elements from the database when you slice it.

    To convert it to a list, use list().

    all_commenter_ids = PhotoComment.objects.filter(which_photo=which_photo).order_by('-id').values_list('submitted_by', flat=True)[:25]
    all_commenter_ids = list(all_commenter_ids)
    

    You might be able to start the queryset from your User model instead of using values_list. You haven't shown your models, so the following code is a guess:

    from django.db.models import Q
    
    commenters = User.objects.filter(Q(id=which_photo.owner_id)|Q(photocomment=which_photo))
    

    0 讨论(0)
提交回复
热议问题