Home > database >  Django rerun signal as created
Django rerun signal as created

Time:02-18

I've a signal running when some model DiseaseCase is saved:

@receiver(post_save, sender=DiseaseCase)
def add_or_edit_map_feature(sender, instance, created, **kwargs):
    ...
    if created:
        do_something()

If I update some fields of one Instance of DiseaseCase Model using Django Shell or Django Admin do_something() will of course not run as the instance is edited but not created.

Let's say I do not want to update the signals code, but still rerun do_something(). How would I override the created boolean for the signal to be true using Django shell?

CodePudding user response:

You can us else or if not created

 @receiver(post_save, sender=DiseaseCase)
    def add_or_edit_map_feature(sender, instance, created, **kwargs):
        ...
        if created: #this signals will be triggered when created any object 
            do_something()
        else: #add your logic for do somethings  

CodePudding user response:

I solved it by just creating a duplicate record. Which works fine in my case.

from d_database.models import DiseaseCase

instances = DiseaseCase.objects.all()
for instance in instances:
    instance.id = None
    instance.save()
  • Related