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

Incorporate with django-import-export #323

Closed
enj opened this issue Jul 12, 2014 · 5 comments
Closed

Incorporate with django-import-export #323

enj opened this issue Jul 12, 2014 · 5 comments

Comments

@enj
Copy link

enj commented Jul 12, 2014

How would I go about making reversion work with django-import-export. Currently imported data has no revisions created for it.

@reichert
Copy link

I created a proxy model to work with import-export and let the original model using reversion.
Probably it os not the best approach, but it worked.
I'd like to hear if someone has a better solution

Enviada do meu iPhone

Em 12/07/2014, às 14:29, enj notifications@github.com escreveu:

How would I go about making reversion work with django-import-export. Currently imported data has no revisions created for it.


Reply to this email directly or view it on GitHub.

@enj
Copy link
Author

enj commented Jul 13, 2014

reichert, could you post some example code on what you did?

@reichert
Copy link

Sure,

here it is:

models.py

class Product(models.Model):
sku = models.CharField(('Product SKU'), max_length=32, blank=True,
help_text=
('Customer product code'))
upc = models.CharField(('Product UPC/EAN'), max_length=16, blank=True,
help_text=
('Product UPC/EAN (bar code)'))
name = models.CharField(('Product Name'), max_length=256, blank=True,
help_text=
('Product Name'))

def __str__(self):
    return self.name

class ProductImportExport(Product):
class Meta:
proxy=True
verbose_name = 'Product Import/Export'
verbose_name_plural = 'Product Import/Export'

admin.py

@admin.register(Product)
class ProductAdmin(reversion.VersionAdmin):
"""
This class is versioned using django-reversion
"""
pass

##################################################################################

DJANGO IMPORT EXPORT CLASSES

#################################################
##################################################################################

class ProductResource(resources.ModelResource):
class Meta:
model = ProductImportExport

@admin.register(ProductImportExport)
class ProductAdminImportExport(ImportExportMixin, ModelAdmin):
resource_class = ProductResource

Alessandro Reichert
Diretor de Operações
CIO Partner Tecnologia da Informação
(11) 4114-0246

On Sun, Jul 13, 2014 at 12:51 PM, enj notifications@github.com wrote:

reichert, could you post some example code on what you did?


Reply to this email directly or view it on GitHub
#323 (comment)
.

@enj
Copy link
Author

enj commented Jul 14, 2014

I replicated this set up but it does not really work. Now I have one admin
view that is aware of all changes (the ImportExport one - cannot revert
anything here) and another that is aware of reversion (so it allows rolling
back to those changes but misses any imports done by import-export). What
I need is a view that can do both at the same time.

@enj
Copy link
Author

enj commented Jul 15, 2014

So I have come up with a working solution using reversion's middleware (though I am sure there is a better way to do this without having to use the middleware):

# admin.py

class ExampleAdmin(ImportExportMixin, reversion.VersionAdmin):
    resource_class = ExampleResource



# models.py

class Example(models.Model):
    date = models.DateField('Date')
    stuff = models.ManyToManyField('stuff')

reversion.register(Example, follow=['stuff',])



# resources.py (could be at end of admin.py)

class ExampleResource(resources.ModelResource):

    class Meta:
        model = Example

    def get_or_init_instance(self, instance_loader, row):

        # Date's val needs to be a datetime object, not unicode
        row['Date'] = datetime.date(*map(int, row['Date'].split('-')))

        return super(ExampleResource, self).get_or_init_instance(instance_loader, row)

    def after_save_instance(self, instance, dry_run):
        if not dry_run:
            reversion.set_comment("Imported with import_export.")



# settings.py

MIDDLEWARE_CLASSES = (
    'reversion.middleware.RevisionMiddleware',
    ...
)

IMPORT_EXPORT_USE_TRANSACTIONS = False # Cannot use True with reversion

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