question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

During import, post_save receiver is called twice

See original GitHub issue

I asked this question on SO but have had no reply so I thought I raise an issue: https://stackoverflow.com/questions/60124600/django-import-export-post-save-called-twice

I created a custom user subclassed from AbstractUser and a post_save signal and a receiver that prints the new user’s id.

@receiver(post_save, sender=CustomUser, dispatch_uid='members.models.customuser.post_save')
def post_save_custom_user(sender, instance=None, created=False, **kwargs):         
    if not created:                                                                
        return                                                                     
    print('post_save_custom_user: {}'.format(instance.id))

When I create a new user via the admin interface the receiver is called once. When I import a user using django-import-export the receiver is called twice: once after the initial Submit of the import file and then again after the Confirm Import. Browsing through the code I see it creates the user in dry_run, rolls back the transaction and creates it again. But how can I tell in my receiver if it’s a dry run or not?

I am using Python 3.6, Django 3.0.3, django-import-export 2.0.1

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:6
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
pratikgajjarcommented, Aug 20, 2020

I have described detailed issue in pr, why it happens : https://github.com/django-import-export/django-import-export/pull/1176 Use this fix, inherit below class

class BaseModelResource(resources.ModelResource):
    save_points = []

    def before_save_instance(self, instance, using_transactions, dry_run):
        """
        Override to add additional logic. Does nothing by default.
        Saving intermediate savepoints of txn
        """
        if using_transactions and dry_run:
            con = transaction.get_connection()
            self.save_points.extend(con.savepoint_ids)

    def after_import(self, dataset, result, using_transactions, dry_run, **kwargs):
        """
        Override to add additional logic. Does nothing by default.
        Manually removing commit hooks for intermediate savepoints of atomic transaction
        """
        if using_transactions and dry_run:
            con = transaction.get_connection()
            for sid in self.save_points:
                con.run_on_commit = [
                    (sids, func) for (sids, func) in con.run_on_commit if sid not in sids
                ]
        super().after_import(dataset, result, using_transactions, dry_run, **kwargs)
2reactions
dco5commented, Mar 17, 2020

i am having the same issue, I am using django-elasticsearch-dsl to index new created objects into elasticsearch, but when I use django-import-export the save post_save signal is triggering twice, so it indexes the imported row twice. Is there a way to disable signals if the import is a dry-run?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Django-import-export post_save called twice - Stack Overflow
When I import a user using django-import-export the receiver is called twice: once after the initial Submit of the import file and then...
Read more >
During import, post_save receiver is called twice - Bountysource
I created a custom user subclassed from AbstractUser and a post_save signal and a receiver that prints the new user's id. @receiver(post_save, ...
Read more >
post_save signal getting called twice ! - Google Groups
Hi I am using django version 1.4.3. I am using two signals in my models.py one is m2m_changed, so i can have a...
Read more >
[Answered]-Django-import-export post_save called twice-django
When I import a user using django-import-export the receiver is called twice: once after the initial Submit of the import file and then...
Read more >
How to stop post_save signal to be run twice? : r/djangolearning
I recently run into a problem resembling this Django-import-export post_save called twice where I defined some post_save function on a ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found