[Answered ]-Converting Django models queryset into a numpy matrix

2👍

You could

  1. Use the values_list() method to get a list of lists. Include the id in the values.
  2. Use list comprehension on the result of the query to remove the ids from the params and generate a list of ids.
  3. Use the params as the parameter for numpy.matrix().
  4. Perform operations on the matrix.
  5. 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()
    

Leave a comment