Dinh Truong Anh Phuong
Dinh Truong Anh Phuong

Reputation: 137

How to send signals in django after import data into model

I am going to send signal to delete all cached after model is updated.

My code work good if I add case by case. It doesnt work when I import file

my model:

class testimport(models.Model):
    id=models.AutoField(primary_key=True)
    so_hd=models.CharField( max_length=50, unique=True)
    ten_kh=models.CharField( max_length=500)
    created_at=models.DateTimeField(auto_now_add=True)
    
# pre_save method
def pre_save_signal_import_redis(sender, instance, created,*args,  **kwargs):
    print(instance.so_hd)
    print("pre_save_signal_import")
    cache.delete("*")

pre_save.connect(pre_save_signal_import_redis,sender=testimport)       

There is no error, It just doesnt print anything even data is updated If I add case by case, pre_save_signal_import_redis work good and print all my view to import code

if request.method == 'POST':
        myfile = request.FILES['myfile']             
        empexceldata = pd.read_excel(myfile, engine='openpyxl')
                        
        dbframe = empexceldata
        df_records = dbframe.to_dict('records')
        batch_size = 1000
        batch = []
        for record in df_records:
            model_instances = testimport(
            so_hd=record['SỐ HD'],
            ten_kh=record["TÊN KH"],

        )
        batch.append(model_instances)
        testimport.objects.bulk_create(batch,batch_size)

Thanks for reading

Upvotes: 0

Views: 121

Answers (1)

Dinh Truong Anh Phuong
Dinh Truong Anh Phuong

Reputation: 137

I found out another way to handle my issue. I just added cache.clear() before I import the database.

Upvotes: 1

Related Questions