Django-import-export: updating model with post_export signal

Created on 10 Oct 2017  路  3Comments  路  Source: django-import-export/django-import-export

Please how do i updated exported Company model below using post_export signal.
I don't want to export items not exported before.

#models.py
class Company(models.Model):
    class Meta:
        verbose_name_plural = "Companies"

    company_name = models.CharField(max_length=254, blank=True)
    website = models.URLField(max_length=254, unique=True)
    address = models.CharField(max_length=254, blank=True, null=True)
    imported = models.BooleanField(default=False)
    exported = models.BooleanField(default=False)
    user = models.ForeignKey(User)

    def __str__(self):
        if self.company_name:
            return self.company_name
        return self.domain

#admin.py
from django.dispatch import receiver from import_export.signals import post_import, post_export

class CompanyResource(resources.ModelResource):
    class Meta:
        model = Company
        fields = ('website', 'user', 'country', 'source', 'industry')

@admin.register(Company)
class CompanyAmin(ImportExportModelAdmin):
    resource_class = CompanyResource
    list_display = ('domain', website', 'exported', 'added_on')
    list_filter = ('user', 'country', 'imported', 'exported', 'added_on')


# IMPORTED ABOVE: from django.dispatch import receiver from import_export.signals import post_import, post_export
@receiver(post_export, dispatch_uid='ss1')
def _post_export(model, **kwargs):
    model.exported = True
    model.save()
question stale

Most helpful comment

@bestofothers Resource class has a method after_export you can override that method to do that rather using signal separately.

Other things for clarification, Do you want to mark each row after export? If so then, You can override get_queryset method also

```python
class CompanyResource(resources.ModelResource):
class Meta:
model = Company
fields = ('website', 'user', 'country', 'source', 'industry')

def get_queryset(self):
queryset = super( CompanyResource, self).get_queryset()
return queryset.filter(exported=False)

def after_export( self, queryset, data, *args, **kwargs )
       queryset.update(exported=True)

I think this will be the best approach rather using signals.

All 3 comments

@bestofothers Resource class has a method after_export you can override that method to do that rather using signal separately.

Other things for clarification, Do you want to mark each row after export? If so then, You can override get_queryset method also

```python
class CompanyResource(resources.ModelResource):
class Meta:
model = Company
fields = ('website', 'user', 'country', 'source', 'industry')

def get_queryset(self):
queryset = super( CompanyResource, self).get_queryset()
return queryset.filter(exported=False)

def after_export( self, queryset, data, *args, **kwargs )
       queryset.update(exported=True)

I think this will be the best approach rather using signals.

great thanks. I used the export_action to solve it but this approach of yours would be better because mine mark exports=True even if I don't download the csv i.e on visiting the export page it already marked items as exported before actually downloading them.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings