Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django "bulk_save" and "bulk_update"

UPDATE: ADDED A BOUNTY. PLEASE PROVIDE AN EXAMPLE AND I WILL ACCEPT THE BEST ANSWER

UPDATE 2: Explicit example now included

Carrying on from the same project, where I asked about bulk_create in a separate thread. I was wondering if there is a way to essentially "bulk_save" - insert if non-existent or simply update if it already exists.

For example:

class Person(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    height = models.DecimalField(blank=True, null=True)
    weight = models.DecimalField(blank=True, null=True)

I have a list of dictionaries with key-value pairs for these fields. I would like to filter by name, and then update the height and/or weight as these my players are still growing and conditioning. If there is no easy way to "bulk_save", a bulk update would also be helpful.

Reference: June 8, 2012 - "get_or_create()" patch at django project

Bulk_update reference

like image 516
snakesNbronies Avatar asked Jul 10 '12 05:07

snakesNbronies


1 Answers

I just did a variation of the update_many function listed below I seem to have improved speeds tremendously already.

http://people.iola.dk/olau/python/bulkops.py

UPDATE - apparently DSE2 is also an option.

https://bitbucket.org/weholt/dse2

Will update with speed tests tomorrow.

like image 91
snakesNbronies Avatar answered Oct 12 '22 22:10

snakesNbronies