You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
**UploadFileForm** is html widget for file upload form in the html page. Then look down at **filehandle**. It is an instance of either ExcelInMemoryUploadedFile or TemporaryUploadedExcelFile, which inherit ExcelMixin and hence have a list of conversion methods to call, such as get_sheet, get_array, etc.:meth:`~django_excel.make_response` converts :class:`~pyexcel.Sheet` instance obtained via :meth:`~django_excel.ExcelMixin.get_sheet` into a csv file for download. Please feel free to change those functions according to :ref:`the mapping table <data-types-and-its-conversion-funcs>`.
94
+
**UploadFileForm** is html widget for file upload form in the html page. Then look down at **filehandle**. It is an instance of either ExcelInMemoryUploadedFile or TemporaryUploadedExcelFile, which inherit ExcelMixin and hence have a list of conversion methods to call, such as get_sheet, get_array, etc.
95
95
96
-
... to be continued ..
96
+
For the response, :meth:`~django_excel.make_response` converts :class:`~pyexcel.Sheet` instance obtained via :meth:`~django_excel.ExcelMixin.get_sheet` into a csv file for download.
97
+
98
+
Please feel free to change those functions according to :ref:`the mapping table <data-types-and-its-conversion-funcs>`.
99
+
100
+
Handle data import
101
+
++++++++++++++++++++++++++++++
102
+
103
+
This example shows how to import uploaded excel file into django models. We are going to import *sample-data.xls*
Except the added "slug" field, **Question** and **Choice** are copied from Django tutoial part 1.
143
+
144
+
Please visit this link http://localhost:8000/import/, you shall see this upload form:
145
+
146
+
.. image:: import-page.png
147
+
148
+
Please then select *sample-data.xls* and upload. Then visit the admin page http://localhost:8000/admin/polls/question, you shall see questions have been populated:
149
+
150
+
.. image:: question-admin.png
151
+
152
+
.. note::
153
+
The admin user credentials are: user name: admin, password: admin
154
+
155
+
And choices too:
156
+
157
+
.. image:: choice-admin.png
158
+
159
+
You may use admin interface to delete all those objects and try again.
160
+
161
+
Now please open views.py and focus on this part of code::
162
+
163
+
def import_data(request):
164
+
if request.method == "POST":
165
+
form = UploadFileForm(request.POST, request.FILES)
The star is :meth:`~django_excel.save_book_to_database`. The parameter **models** can be a list of django models or a list of tuples, each of which contains:
185
+
186
+
1. django model (**compulsory**)
187
+
2. an array of model fields or a dicionary of key maps
188
+
3. custom formating fuction
189
+
4. the index of the row that has the field names
190
+
5. the index of the column that has the field names
191
+
192
+
When an array of model fields is supplied in the second member in the tuple, the names of the supplied fields should match the field names of the corresponding django model(the first member in the tuple) and the sequence of the supplied fields shall match the one in the uploaded excel sheet. For example::
When a dictionary of key maps is supplied, its keys should be the field names in the uploaded excel sheet and the value should be the actual field name in the corresponding django model. For example::
197
+
198
+
(Question,{"Question Text": "question_text",
199
+
"Publish Date": "pub_date",
200
+
"Unique Identifier": "slug"}, None, 0)
201
+
202
+
The custom formatting function is needed when the data from the excel sheet needs translation before data import. For example, **Choice** has a foreign key to **Question**. When choice data are to be imported, "Question" column needs to be translated to a question instance. In our example, "Question" column in "Sheet 2" contains the values appeared in "Unique Identifier" column in "Sheet 1".
203
+
204
+
Handle data export
205
+
++++++++++++++++++++++++++++++
206
+
207
+
This section shows how to export the data in your models as an excel file. After you have completed the previous section, you can visit http://localhost:8000/export/book and you shall get a file download dialog:
208
+
209
+
.. image:: download-dialog.png
210
+
211
+
Please save and open it. You shall see these data in your window:
:meth:`~django_excel.make_response_from_tables` does all what is needed: read out the data, convert them into xls and give it the browser. And what you need to do is to give a list of models to be exported and a file type. As you have noticed, you can visit http://localhost:8000/exportsheet and will get **Question** exported as a single sheet file.
0 commit comments