My app is getting killed by adding m2m relationships.  I can bulk create 
the objects but for the m2m I have to iterate over them which is super 
slow.  I'm looking for a way to bulk add the m2m relationships.   

Here is a example:
 class Sensor(models.Model):
    Name     = models.CharField( max_length=200 )
    Value    = models.FloatField()

class DataPoint(BaseModel):
    Taken_datetime = models.DateTimeField( blank=True, null=True ) 
    Sensors        = models.ManyToManyField( SensorVal, blank=True, 
null=True )    

for row in rows:
    dp = DataPoint.objects.get(Taken_datetime=row['date']) 

    sensorToAdd = []
    for sensor in sensors:
        s = Sensor.objects.get(Name=sensor.name, Value=sensor.value  )
        sensorToAdd.append( s )

    dp.Sensors.add( sensorToAdd )

In the actually app I bulk create all the DataPoint and Sensor instances.  
The problem is that |dp.Sensors.add( sensorToAdd )| does a lot of hits on 
the db.  I want a way bulk add all the sensors.  

Can I bulk add the m2m relationships in Django?  If not, can this be done 
in SQL and can you give me some pointers about where to get started?  I'm 
still a  

I'm a beginner with SQL but it seems like there should be a way to group 
all these calls together instead of creating one at a time.  

I'm using sqlite for development and postgres on the server.  

Here is the stack overflow I posted a few days ago: 
http://stackoverflow.com/questions/31907116/bulk-add-m2m-relationship-for-multiple-instances

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/4b1c76b1-bd25-440f-b888-07f7a9a0a033%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to