tags:

views:

252

answers:

2

How do I convert a django QuerySet to numpy record array?

PS: I know you can iterate and construct it and but is there any other cleaner solution?

+4  A: 

This is like asking "how do I convert the contents of my fridge into dinner?". It depends on what you have in your fridge and what you'd like to eat. The short answer (equivalent to saying "by cooking") is to iterate over the queryset, constructing objects of whatever composite data types you'd like to instantiate the array with (generally an iterable and a dictionary). The long answer depends on what you'd actually like to accomplish.

ozan
Mmm... dinner...
Daniel Roseman
Thanks for your advice! but I wouldn't have posted this question for that obvious answer :).
Vishal
+1  A: 

What I was looking for:

  1. From QuerySet qs get vlqs (django.db.models.query.ValuesListQuerySet)

    vlqs = qs.values_list()

  2. Covert vlqs to list

    mylist = list(vlqs)

  3. Create numpy record array

    r = np.core.records.array(mylist, names='field1, field2, field3') // Names are the model fields

Vishal