In Django, what's the difference between the following two:
Article.objects.values_list('comment_id', flat=True).distinct()
vs
Article.objects.values('comment_id').distinct()
My goal is to get a list of unique comment ids under each Article
. I've read the documentation (and in fact have used both approaches). The results overtly seem similar.
values()
Returns a QuerySet that returns
dictionaries
, rather than model instances, when used as an iterable.values_list()
Returns a QuerySet that returns
list of tuples
, rather than model instances, when used as an iterable.distinct()
distinct are used to
eliminate the duplicate
elements.Example:
The
values()
method returns a QuerySet containing dictionaries:The
values_list()
method returns a QuerySet containing tuples:If you are using
values_list()
with a single field, you can useflat=True
to return a QuerySet of single values instead of 1-tuples:You can get the different values with: