Retrieving objects created in trasaction

35 views
Skip to first unread message

roboslone -

unread,
Aug 16, 2017, 2:29:09 PM8/16/17
to django...@googlegroups.com
Hi,

I have some function (fetcher) that runs in transaction (it has to) and it creates a bunch if new objects that are written to database after transaction is commited. Those objects are added to a many-to-many relation. And on that relation change a signal is fired and handled by my signal handler, that collects objects from relation and writes them into some cache.

The problem is that during signal handling newly created objects do have PKs, but are not in database yet. So signal_handler in code below always gets empty queryset.


def fetcher():
with transaction.atomic():
foo = Foo.objects.create(...)
foo.bars.add(
Bar.objects.create(...),
Bar.objects.create(...),
Bar.objects.create(...),
...
)

@receiver(m2m_changed, sender=Foo.bars.through)
def signal_handler(pk_set=None, **kwargs):
# signal_handler is being called inside of a transaction block
Bar.objects.filter(pk__in=pk_set) # <-- returns empty QuerySet!


Is there a way to delay signal handling until transaction is committed or to get newly created objects by PKs?

pieceof...@gmail.com

unread,
Aug 23, 2017, 2:56:18 PM8/23/17
to Django users
Try foo.save() before foo.bars.add().

James Schneider

unread,
Aug 23, 2017, 3:07:35 PM8/23/17
to django...@googlegroups.com
Perhaps you can try nesting the transactions?


-James

Christian Ledermann

unread,
Aug 24, 2017, 7:09:59 AM8/24/17
to django...@googlegroups.com
https://docs.djangoproject.com/en/1.9/topics/db/transactions/#performing-actions-after-commit

>
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/F1A382BE-8C18-4D5B-8758-C42C18A7FC94%40gmail.com.
> For more options, visit https://groups.google.com/d/optout.



--
Best Regards,

Christian Ledermann

Newark-on-Trent - UK
Mobile : +44 7474997517

https://uk.linkedin.com/in/christianledermann
https://github.com/cleder/


<*)))>{

If you save the living environment, the biodiversity that we have left,
you will also automatically save the physical environment, too. But If
you only save the physical environment, you will ultimately lose both.

1) Don’t drive species to extinction

2) Don’t destroy a habitat that species rely on.

3) Don’t change the climate in ways that will result in the above.

}<(((*>

Daniel Hepper

unread,
Aug 24, 2017, 11:47:39 AM8/24/17
to Django users
I cannot reproduce this behavior with Django 1.11 and PostgreSQL 9.6. What database are you using?

Does your Bar model maybe use non-standard Manager? Can you actually fetch to objects after the transaction has finished?

"Александр Христюхин (roboslone)"

unread,
Sep 11, 2017, 10:53:17 AM9/11/17
to django...@googlegroups.com
Thanks to everyone and sorry for the delay, I've missed your responses.

I've tried to use transaction.on_commit, it didn't work for me. For some strange reason Django thought the code wasn't running in a transaction.

I'm using Django 1.10 and PostgreSQL 9.6. Bar uses standard manager and yes, I can fetch objects after transaction has finished.

I ended up with local storage for newly created objects. So I query database for existing objects I've added to relation and if original pk_set is bigger I also query that local storage. That fixed the problem for me, but it's not exactly a good solution.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages