Reputation: 14656
Consider the following Django model:
class SomeModel(models.Model):
some_var = models.IntergerField()
Whats the best way to get an array of a models parameters from a search results?
Example (not working of course):
a = SomeModel.objects.all().some_var
would give something like [3, 4, 1, 9, 1, 2]
Upvotes: 1
Views: 3645
Reputation: 590
You can yield the list, this would like the following:
foo = [i.some_var for i in SomeModel.objects.all()]
But I've been notified that this would waste the memory.
Upvotes: 0
Reputation: 15400
There is existing queryset method called values_list
https://docs.djangoproject.com/en/1.10/ref/models/querysets/#values-list
a = SomeModel.objects.all().values_list('some_var', flat=True)
We are using flat=True
so that it will give flat list
Entry.objects.values_list('id').order_by('id')
[(1,), (2,), (3,), ...]
Entry.objects.values_list('id', flat=True).order_by('id')
[1, 2, 3, ...]
Upvotes: 5