Luiz Carlos Macedo
Luiz Carlos Macedo

Reputation: 29

Why using bulk_create on Django to insert data with foreign keys returns "property object is not callable"?

I'm using Django 2.17 with a SQLite 3.26 database and trying to insert data from a csv file. I was using the get_or_create method, but it was too slow. So I start to try to insert using bulk_create.

I have the following fields of Models being used:

class SENSOR_TEMPERATURA(models.Model):
    ID_Sensor_Temperatura = models.AutoField(primary_key = True)

class VALOR_TEMPERATURA(models.Model):
    ID_Valor_Temperatura = models.AutoField(primary_key = True)
    ID_Sensor_Temperatura = models.ForeignKey(SENSOR_TEMPERATURA, on_delete = models.PROTECT, null = False, db_column = 'VATE_CD_ID_Sensor_Temperatura')
    Data_De_Medição = models.DateTimeField(default = datetime.now(), null = False)
    Valor = models.DecimalField(default = 0, null = False, max_digits = 30, decimal_places = 15)

The code that I'm trying to run is:

     print (datetime.datetime.now())         
     reader = csv.reader(f) 
     insert_CSV = [] 
     count = 1 
     for row in reader: 
         insert_CSV.append([ 
         VALOR_TEMPERATURA.pk(ID_Valor_Temperatura = count), 
         VALOR_TEMPERATURA(Data_De_Medição = datetime.datetime.strptime(row[0] + " UTC-0300",'%d/%m/%Y %H:%M:%S %Z%z')), 
         VALOR_TEMPERATURA(Valor = float(row[1])), 
         VALOR_TEMPERATURA(ID_Sensor_Temperatura = SENSOR_TEMPERATURA.objects.get(ID_Sensor_Temperatura = 4)) 
         ]) 
         count = count + 1 
     print (datetime.datetime.now()) 
     VALOR_TEMPERATURA.objects.bulk_create(insert_CSV) 
     print (datetime.datetime.now()) 

The part that I think is put me in trouble is "ID_Sensor_Temperatura = SENSOR_TEMPERATURA.objects.get(ID_Sensor_Temperatura = 4))", but it is exactly how I defined the Foreign Key when using get_or_create, so I can't figure out what is the problem.

I'm getting the following error:

  6     for row in reader:
  7         insert_CSV.append([
  8         VALOR_TEMPERATURA.pk(VATE_CD_ID_Valor_Temperatura = count),
  9         VALOR_TEMPERATURA(VATE_DF_Data_De_Medição = datetime.datetime.strptime(row[0] + " UTC-0300",'%d/%m/%Y %H:%M:%S %Z%z')),
 10         VALOR_TEMPERATURA(VATE_VL_Valor = float(row[1])),

TypeError: 'property' object is not callable

What may be the problem?

Upvotes: 0

Views: 262

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599490

This isn't how you write Python. You need to create an instance of the object, passing it the values.

insert_CSV.append( 
     VALOR_TEMPERATURA(
        ID_Valor_Temperatura=count, 
         Data_De_Medição=datetime.datetime.strptime(row[0] + " UTC-0300",'%d/%m/%Y %H:%M:%S %Z%z')), 
         Valor=float(row[1]), 
         ID_Sensor_Temperatura=SENSOR_TEMPERATURA.objects.get(ID_Sensor_Temperatura=4)
     )
)

Note also, your models should not be defined in ALL_CAPS, as they are not constants. They shoudl be called ValorTemperatura and SensorTemperatura.

Upvotes: 0

Related Questions