Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

post_save signal of TaskResult doesn't trigger #395

Open
Legend94rz opened this issue Jun 8, 2023 · 2 comments
Open

post_save signal of TaskResult doesn't trigger #395

Legend94rz opened this issue Jun 8, 2023 · 2 comments

Comments

@Legend94rz
Copy link

I have custom model that contains a TaskResult as OneToOneField:

class MyModel(models.Model):
    task = models.OneToOneField(TaskResult, on_delete=models.SET_NULL, null=True)
    ...

in my view, I do something like:

@receiver(post_save, sender=TaskResult, dispatch_uid='task_update')
def _on_task_save(sender, instance, **kwargs):
    print("POST_SAVE: ", instance.task_id, instance.id)

def view(request):
    form = models.MyModelForm(data=request.POST)
    if form.is_valid():
        obj :models.MyModel = form.instance
        result = my_celery_task.delay(obj.xxx)
        obj.save()
    return render(request, 'app/index.html')

Then I found the _on_task_save is nerver trigger, but the data is written into the db, which could be checked by python manage.py shell and then run TaskResult.objects.all().

I have found a similar issue #41, but it doesn't solve my problem.

Thanks for any help!

@hetvi01
Copy link

hetvi01 commented Dec 22, 2023

I have used this same approach and it's working for me, just cross verify the below things:

  1. Confirm that the data is being correctly stored in your database by checking for the entry in celery task results table.
  2. Ensure that you've connected the signals file to your app.py file and have included it in the ready function to enable the use of signals.

If everything is set up correctly, the print statement should appear in the terminal where your Celery worker is running.

@FraCata00
Copy link

FraCata00 commented Apr 18, 2024

Follow the instructions by @hetvi01

Example:

app_label -> example

your_project.your_application.apps.py

from django.apps import AppConfig


class ExampleConfig(AppConfig):
    default_auto_field = "django.db.models.BigAutoField"
    name = "example"
    verbose_name = "Examples"

    def ready(self) -> None:
        import example.signals  # noqa: F401

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants