Home > Back-end >  More efficient way to update multiple model objects each with unique values
More efficient way to update multiple model objects each with unique values

Time:11-14

I am looking for a more efficient way to update a bunch of model objects. Every night I have background jobs creating 'NCAABGame' objects from an API once the scores are final.

In the morning I have to update all the fields in the model with the stats that the API did not provide.

As of right now I get the stats formatted from an excel file and I copy and paste each update and run it like this:

    NCAABGame.objects.filter(
        name__name='San Francisco', updated=False).update(
        field_goals=38,
        field_goal_attempts=55,
        three_points=11,
        three_point_attempts=24,
        ...
    )

The other day there were 183 games, most days between 20-30 so it can be very timely doing it this way. I've looked into bulk_update and a few other things but I can't really find a solution. I'm sure there is something simple that I'm just not seeing.

I appreciate any ideas or solutions you can offer.

CodePudding user response:

If you need to update each object that gets created via the API manually anyway, I would not even bother going through Django. Just load your games from the API directly in Excel, then make your edits in Excel, and save as CSV file. Then I would add the CSV directly into the database table, unless there is a specific reason that objects must be created via Django? I mean, you can of course do that with something like the below, which could be modified to also work for your current method via updates, but then you need to first retrieve the correct pk of the object that you want to update.

import csv
  
with open("my_data.csv", 'r') as my_data_file:
    reader = csv.reader(my_data_file) 
    for row in reader:

        # get_or_create returns a tuple. 'created' is a boolean that indicates
        # if a new object was created or not, with game holding the object that
        # was either retrieved or created

        game, created = NCAABGame.objects.get_or_create(
            name=row[0],
            field_goals=row[1],
            field_goal_attempts=row[2],
            ....,
            )
  • Related