Code Monkey home page Code Monkey logo

django-import-export-celery's Introduction

image

django-import-export-celery: process slow django imports and exports in celery

django-import-export-celery helps you process long running imports and exports in celery.

Basic installation

  1. Set up celery to work with your project.
  2. Add 'import_export_celery' to your INSTALLED_APPS settings variable
  3. Add 'author.middlewares.AuthorDefaultBackendMiddleware' to your MIDDLEWARE_CLASSES
  4. Configure the location of your celery module setup

    IMPORT_EXPORT_CELERY_INIT_MODULE = "projectname.celery"

Setting up imports with celery

A fully configured example project can be found in the example directory of this repository.

  1. Perform the basic setup procedure described above.
  2. Configure the IMPORT_EXPORT_CELERY_MODELS variable.

    def resource():  # Optional
        from myapp.models import WinnerResource
        return WinnerResource
    
    
    IMPORT_EXPORT_CELERY_MODELS = {
        "Winner": {
            'app_label': 'winners',
            'model_name': 'Winner',
            'resource': resource,  # Optional
        }
    }

    The available parameters are app_label, model_name, and resource. 'resource' should be a function which returns a django-import-export Resource.

  3. Done

Preforming an import

You will find an example django application that uses django-import-export-celery for importing data. There are instructions for running the example application in the example directory's README file. Once you have it running, you can perform an import with the following steps.

  1. Navigate to the example applications admin page:

    image

  2. Navigate to the ImportJobs table:

    image

  3. Create a new import job. There is an example import CSV file in the example/example-data directory. Select that file. Select csv as the file format. We'll be importing to the Winner's model table.

    image

  4. Select "Save and continue editing" to save the import job and refresh until you see that a "Summary of changes made by this import" file has been created.

    image

  5. You can view the summary if you want. Your import has NOT BEEN PERFORMED YET!

    image

  6. Return to the import-jobs table, select the import job we just created, and select the "Perform import" action from the actions drop down.

    image

  7. In a short time, your imported Winner object should show up in your Winners table.

    image

Setting up exports

As with imports, a fully configured example project can be found in the example directory.

  1. Add a export_resource_classes classmethod to the model you want to export.
    @classmethod
    def export_resource_classes(cls):
        return {
            'winners': ('Winners resource', WinnersResource),
            'winners_all_caps': ('Winners with all caps column resource', WinnersWithAllCapsResource),
        }

    This should return a dictionary of tuples. The keys should be unique unchanging strings, the tuples should consist of a resource and a human friendly description of that resource.

  2. Add the create_export_job_action to the model's ModelAdmin.
    from django.contrib import admin
    from import_export_celery.admin_actions import create_export_job_action
    
    from . import models
    
    
    @admin.register(models.Winner)
    class WinnerAdmin(admin.ModelAdmin):
        list_display = (
            'name',
        )
    
        actions = (
            create_export_job_action,
        )
  3. To customise export queryset you need to add get_export_queryset to the ModelResource.
    class WinnersResource(ModelResource):
        class Meta:
            model = Winner
    
        def get_export_queryset(self):
            """To customise the queryset of the model resource with annotation override"""
            return self.Meta.model.objects.annotate(device_type=Subquery(FCMDevice.objects.filter(
                    user=OuterRef("pk")).values("type")[:1])
  4. Done!

Performing exports with celery

  1. Perform the basic setup procedure described in the first section.
  2. Open up the object list for your model in django admin, select the objects you wish to export, and select the Export with celery admin action.
  3. Select the file format and resource you want to use to export the data.
  4. Save the model
  5. You will receive an email when the export is done, click on the link in the email
  6. Click on the link near the bottom of the page titled Exported file.

For developers of this library

You can enter a preconfigured dev environment by first running make and then launching ./develop.sh to get into a docker compose environment packed with redis, celery, postgres and everything you need to run and test django-import-export-celery.

Before submitting a PR please run flake8 and (in the examples directory) python3 manange.py test.

Please note, that you need to restart celery for changes to propogate to the workers. Do this with docker-compose down celery, docker-compose up celery.

Comercial support

Comercial support is provided by gradesta s.r.o.

Credits

django-import-export-celery was developed by the Czech non-profit auto*mat z.s..

django-import-export-celery's People

Contributors

ahmedalzabidi avatar alaanour94 avatar aparakian avatar charleshan avatar denkneb avatar dependabot[bot] avatar frabyn avatar marksweb avatar petrdlouhy avatar petrkudy avatar platzhersh avatar q0w avatar rmaceissoft avatar timthelion avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.