Показаны сообщения с ярлыком excel. Показать все сообщения
Показаны сообщения с ярлыком excel. Показать все сообщения

суббота, 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

django импорт excel

Появилась очень удобная система для импорта данных из excel в django:

Чтобы начать использовать это приложение django в своем проекте:

1. Переходим на страницу https://github.com/satels/django-import-excel и нажимаем кнопку Watch
2.Скачиваем проект себе https://github.com/satels/django-import-excel/zipball/master
3. Распаковываем, переходим в папку и запускаем pip install -e . или же python setup.py install
4. В settings.py:

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

5. Итак, например, нам надо импортировать данные из excel для модели


class Book(models.Model):

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


Файл excel же содержит данные

name | author
Мастер и Маргарита | Михаил Булгаков
И дольше века длится день | Чингиз Айтматов

Для этого создаем форму

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



class BookImportForm(ImportExcelForm):

    @transaction.autocommit
    def update_callback(self, request, converted_data):
         for book_item in converted_data[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'),
),


Видим здесь мы указываем форму для загрузки книг, next_url - страницу для перехода после успешной загрузки, with_good - это значит, что надо подтвердить загружаемые данные с предварительным просмотром, также вы можете указывать template_name, если захотите задать свой (скопируйте файл из import_excel/templates/import_excel/import_excel.html)

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

django-excel - отдаем excel красиво

Старая классная вещь (2010 года) от kmike - django-excel-response Для того, чтобы отдать excel из django как http response - просто составляем список списков, или же передаем объекты модели django и получаем готовый excel Ссылка: https://bitbucket.org/kmike/django-excel-response

Для загрузки же используйте django-import-excel