2👍
You could
- Use the
values_list()
method to get a list of lists. Include theid
in the values. - Use list comprehension on the result of the query to remove the ids from the
params
and generate a list ofids
. - Use the
params
as the parameter fornumpy.matrix()
. - Perform operations on the matrix.
-
Iterate over the ids and save the values from each row of the matrix to the corresponding object.
import numpy as np qs = models.Devices.objects.all() params = qs.values_list('id', 'b1', 'b2', 'b3', 'b4', 'b5', 'b6', 'b7', 'b8') ids = [arr.pop(0) for arr in params] matrix = np.matrix(params) # perform operations here for index, id in enumerate(ids): row = matrix[index].flat obj = qs.get(id=id) obj.b1 = row.next() obj.b2 = row.next() obj.b3 = row.next() obj.b4 = row.next() obj.b5 = row.next() obj.b6 = row.next() obj.b7 = row.next() obj.save()
Source:stackexchange.com