Giter VIP home page Giter VIP logo

django-devour's Introduction

Django Devour

Django Devour helps importing files into your database - for instance CSV. It lets you define the logic for the data import in a custom importer module. It gives you a Django management command that points to your importer module. It also provides a way to add an import button in the admin.

Installation

Install the package using something like pip and add devour to your INSTALLED_APPS setting.

You can run tests for django-devour this way:

./manage.py test tests --settings=devour.tests.settings

Importer Module

The importer is class that defines an import method. For instance, AccountCSVHandler is an importer that defines import_data(data). It's expected to be called this way:

 AccountCSVHandler.import_data("accounts.csv")

You can use django-adaptors to build a CSV data importer. It gives you the import_data() method.

Management Command

The management command expect the file name and the dotted path to the importer module:

./manage.py devour_file <file_name> <importer>
./manage.py devour_file accounts.csv accounts.importers.AccountCSVHandler

In the following example, we will define a Account Django model that stores first and last names. The importer will be using django-adaptors to build a CSV importer. Point

# accounts/models.py

from django.db import models

class Account(models.Model):
    first_name = models.CharField(max_length=255)
    last_name = models.CharField(max_length=255)

    def __unicode__(self):
        return '%s %s' % (self.first_name, self.last_name)



# accounts/importers.py

from adaptor import fields
from adaptor.model import CsvModel
from accounts.models import Account

class AccountCSVHandler(CsvModel):
    first_name = fields.CharField()
    last_name = fields.CharField()

    class Meta:
        delimiter = ","
        dbModel = Account
        silent_failure = False


# In the shell

./manage.py devour_file accounts.csv accounts.importers.AccountCSVHandler

Import From The Admin

You can add an upload / import button in your admin page. This will call the importer that was specified in the ModelAdmin class.

django-devour admin

# accounts/admin.py

from django.contrib import admin
from devour.admin import FileImportAdmin
from accounts.importers import AccountCSVHandler
from accounts.models import Account

class AccountAdmin(FileImportAdmin, admin.ModelAdmin):
    list_display = ('first_name', 'last_name')
    importer = AccountCSVHandler

admin.site.register(Account, AccountAdmin)

The admin class is expected to have a importer attribute that can be either the importer class or a dotted path to it.

Here for instance, we will have importer = 'accounts.importers.AccountCSVHandler'

Settings

Import Method Name

By default, we use import_data as the import method name. This can be changed:

DEVOUR_IMPORT_METHOD_NAME = 'my_custom_import'

With this setting, the AccountCSVHandler importer will be called this way:

 AccountCSVHandler.my_custom_import("accounts.csv")

django-devour's People

Contributors

dionysio avatar

Watchers

 avatar  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.