Django objects.filter() values_list() vs python list comprehension for __in query?

Try l_magazines. Values_list('id', flat=True) . That returns a list of ids instead of a list of single id tuples.

Up vote 2 down vote favorite share g+ share fb share tw.

I have a quirk(?) with Django queryset filtering: ipdb> MagazineIssue.objects. Filter(id__in=l_magazines. Values_list('id')) Out0: or ipdb> MagazineIssue.objects.

Filter(id__in=l_magazine. Id for l_magazine in l_magazines) Out0: and ipdb> l_magazines. Values_list('id') Out0: (1,) ipdb> l_magazine.

Id for l_magazine in l_magazines Out0: 1 so, how to use values_list()? (to produce): 1 or is python list comprehension the 'way to go'? Python django django-models django-queryset link|improve this question asked Jan 27 '11 at 1:10Daryl13410 64% accept rate.

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: values/values_list will yield the actual value stored in the field if the value is a foreign key, and you have the appropriate relations set up in your model, the list comprehension will give you the object referred to by the foreign key. Choosing the wrong one will either result in unnecessary database hits, or unnecessary faffing around, depending on what you are trying to do.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions