суббота, 10 декабря 2011 г.

django import excel

This publication is present of django system for import data from excel:

Use this step by step for rapid start use wih django application:

1. Follow to page https://github.com/satels/django-import-excel and push the button Watch
2.Download this project https://github.com/satels/django-import-excel/zipball/master
3. Unpack, following to the folder and run pip install -e . or python setup.py install
4. In settings.py:

INSTALLED_APPS = [
     ...
     'import_excel',
     ...
]

5. So, for example, you need  import the data from excel for model


class Book(models.Model):

    name = models.CharField(max_length=255)
    author = models.CharField(max_length=255)


The excel file contains the data

name | author
Tom Sawyer | Mark Twain
The Sands of Mars | Arthur C. Clarke

To do is, create this form:

from django.db import transaction
from import_excel.forms import ImportExcelForm


class BookImportForm(ImportExcelForm):

    @transaction.autocommit
    def update_callback(self, request, converted_items):
         for book_item in converted_items[:1]:
              name = book_item[0]
              author = book_item[1]
              Book.objects.create(name=name, author=author)


Далее, в urls.py:

urlpatterns = patterns('',
      url(r'^/books/import-from-excel/$', permission_required('books.add_book')(import_excel), {
             'FormClass': BookImportForm, 'next_url': '/books/', 'with_good': True, 'template_name': 'import_excel/import_excel.html',
      }, name='book-import-excel'),
),


We see this in import book form , next_url - the url of page for redirect after succefull import request, with_good - it means, what must pre moderate data from excel file, also you must set template_name, if you have custom template (copy file from import_excel/templates/import_excel/import_excel.html)

P.S. permission_required for django admin zone

Комментариев нет:

Отправить комментарий