Does Django queryset values_list call a list object?

I have a Django app where users post photos and others post comments on photos.

When a comment is left, I need to notify:

  • Everyone else who wrote in this thread
  • The owner of the photo, if they are not included in (1)

For (1):

#I slice by 25 because I arbitrarily deem anyone beyond that irrelevant. all_commenter_ids = PhotoComment.objects.filter(which_photo=which_photo).order_by('-id').values_list('submitted_by', flat=True)[:25] 

Next, for (2) I try:

  all_relevant_ids = all_commenter_ids.append(which_photo.owner_id) all_relevant_ids = list(set(all_relevant_ids)) 

As a result, I get an error message:

'ValuesListQuerySet' object does not have 'append' attribute

I find this strange because I am retrieving values_list .

Isn't that a list object, in which case the append attribute should not work in this scenario? Please explain what is wrong and suggest alternatives.

+5
source share
1 answer

The values_list method returns a ValuesListQuerySet . This means that it has the advantages of a set of queries. For example, it is lazy, so you only retrieve the first 25 items from the database when you cut 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 may be able to run a set of queries from your User model instead of values_list . You did not show your models, so the following code is an assumption:

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

All Articles