I have a quirk(?) with Django queryset filtering:
ipdb> MagazineIssue.objects.filter(id__in=l_magazines.values_list('id'))
Out[0]: []
or
ipdb> MagazineIssue.objects.filter(id__in=[l_magazine.id for l_magazine in l_magazines])
Out[0]: [<MagazineIssue: Architecture Australia, Jan 1995 (#1)>]
and
ipdb> l_magazines.values_list('id')
Out[0]: [(1,)]
ipdb> [l_magazine.id for l_magazine in l_magazines]
Out[0]: [1]
so, how to use values_list()? (to produce):
[1]
or is python list comprehension the 'way to go'?
Try l_magazines.values_list('id', flat=True)
. That returns a list of ids instead of a list of single id tuples.
One thing to note is that there is a difference in the behaviour of values/values_list from a list comprehension:
Choosing the wrong one will either result in unnecessary database hits, or unnecessary faffing around, depending on what you are trying to do.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.