Why am I getting DoesNotExist for the parent model in save_related

23 views Asked by At

I am firing a celery task in save_related that takes the parent object id and does a get query to obtain the instance for use.

def save_related(self, request, form, formsets, change):
    ...
    # Some stuff happens earlier here that don't access the db
    args = (var1, var2, var3, form.instance.id)
    logger.debug(f"Sending stuff to celery for bulk update")
    jobs = group(
    bulk_update_stuff_in_admin.s(ids_chunk, *args) for ids_chunk in chunks(ids, 128)
    )
    jobs()
    super().save_related(request, form, formsets, change)


@celery_app.task
def bulk_update_stuff_in_admin(var1, var2, var3, parent_id):
    parent = ParentModel.objects.get(id=parent_id)
    ...

Problem is some of the get queries are failing with DoesNotExist. This doesn't make sense to me because the Django docs clearly state that "Note that at this point the parent object and its form have already been saved.". Also why are others not failing?

I am about to experiment with what happens when I call super().save_related(...) before the celery task but I don't think that should be an issue because of the note above.

Django==4.2.3

0

There are 0 answers