Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Django queryset values_list return a list object?

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 wrote in this thread
  2. The owner of the photo, in case they're not included in (1)

For (1), I do:

#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)) 

I end up with an error:

'ValuesListQuerySet' object has no attribute 'append'

I find this strange, because I'm extracting a values_list.

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

like image 685
Hassan Baig Avatar asked May 10 '16 13:05

Hassan Baig


People also ask

What does Django values_list return?

values_list() This is similar to values() except that instead of returning dictionaries, it returns tuples when iterated over. Each tuple contains the value from the respective field or expression passed into the values_list() call — so the first item is the first field, etc.

What does QuerySet filter return?

The filter() method is used to filter you search, and allows you to return only the rows that matches the search term.

Is a QuerySet a list?

A QuerySet is, in essence, a list of objects of a given Model. QuerySets allow you to read the data from the database, filter it and order it.

How does QuerySet work in Django?

A QuerySet is a collection of data from a database. A QuerySet is built up as a list of objects. QuerySets makes it easier to get the data you actually need, by allowing you to filter and order the data.


Video Answer


1 Answers

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)) 
like image 99
Alasdair Avatar answered Sep 20 '22 19:09

Alasdair