1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
|
==============================
Installation and configuration
==============================
import-export is available on the Python Package Index (PyPI), so it
can be installed with standard Python tools like ``pip`` or ``easy_install``::
pip install django-import-export
This will automatically install the default formats supported by tablib.
If you need additional formats you should install the extra dependencies as required
appropriate tablib dependencies (e.g. ``pip install django-import-export[xlsx]``).
To install all available formats, use ``pip install django-import-export[all]``.
For all formats, see the
`tablib documentation <https://tablib.readthedocs.io/en/stable/formats.html>`_.
Alternatively, you can install the git repository directly to obtain the
development version::
pip install -e git+https://github.com/django-import-export/django-import-export.git#egg=django-import-export
Now, you're good to go, unless you want to use import-export from the
admin as well. In this case, you need to add it to your ``INSTALLED_APPS`` and
let Django collect its static files.
.. code-block:: python
# settings.py
INSTALLED_APPS = (
...
'import_export',
)
.. code-block:: shell
$ python manage.py collectstatic
All prerequisites are set up! See :doc:`getting_started` to learn how to use
import-export in your project.
Settings
========
You can configure the following in your settings file:
``IMPORT_EXPORT_USE_TRANSACTIONS``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Controls if resource importing should use database transactions. Defaults to
``True``. Using transactions makes imports safer as a failure during import
won’t import only part of the data set.
Can be overridden on a ``Resource`` class by setting the
``use_transactions`` class attribute.
``IMPORT_EXPORT_SKIP_ADMIN_LOG``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If set to ``True``, skips the creation of admin log entries when importing via the
:ref:`Admin UI<admin-integration>`.
Defaults to ``False``. This can speed up importing large data sets, at the cost
of losing an audit trail.
Can be overridden on a ``ModelAdmin`` class inheriting from ``ImportMixin`` by
setting the ``skip_admin_log`` class attribute.
.. _import_export_tmp_storage_class:
``IMPORT_EXPORT_TMP_STORAGE_CLASS``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
A string path to the preferred temporary storage module.
Controls which storage class to use for storing the temporary uploaded file
during imports. Defaults to ``import_export.tmp_storages.TempFolderStorage``.
Can be overridden on a ``ModelAdmin`` class inheriting from ``ImportMixin`` by
setting the ``tmp_storage_class`` class attribute.
.. _import_export_default_file_storage:
``IMPORT_EXPORT_DEFAULT_FILE_STORAGE``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
A string path to a customized storage implementation.
This setting is deprecated and only applies if using Django with a version less than 4.2,
and will be removed in a future release.
.. _import_export_import_permission_code:
``IMPORT_EXPORT_IMPORT_PERMISSION_CODE``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If set, lists the permission code that is required for users to perform the
'import' action. Defaults to ``None``, which means all users can perform
imports.
Django’s built-in permissions have the codes ``add``, ``change``, ``delete``,
and ``view``. You can also add your own permissions. For example, if you set this
value to 'import', then you can define an explicit permission for import in the example
app with:
.. code-block:: python
from core.models import Book
from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType
content_type = ContentType.objects.get_for_model(Book)
permission = Permission.objects.create(
codename="import_book",
name="Can import book",
content_type=content_type,
)
Now only users who are assigned 'import_book' permission will be able to perform
imports. For more information refer to the
`Django auth <https://docs.djangoproject.com/en/stable/topics/auth/default/>`_
documentation.
.. _import_export_export_permission_code:
``IMPORT_EXPORT_EXPORT_PERMISSION_CODE``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Defines the same behaviour as :ref:`IMPORT_EXPORT_IMPORT_PERMISSION_CODE`, but for
export.
``IMPORT_EXPORT_CHUNK_SIZE``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
An integer that defines the size of chunks when iterating a QuerySet for data
exports. Defaults to ``100``. You may be able to save memory usage by
decreasing it, or speed up exports by increasing it.
Can be overridden on a ``Resource`` class by setting the ``chunk_size`` class
attribute.
.. _import_export_skip_admin_confirm:
``IMPORT_EXPORT_SKIP_ADMIN_CONFIRM``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If ``True``, no import confirmation page will be presented to the user in the Admin UI.
The file will be imported in a single step.
By default, the import will occur in a transaction.
If the import causes any runtime errors (including validation errors),
then the errors are presented to the user and then entire transaction is rolled back.
Note that if you disable transaction support via configuration (or if your database
does not support transactions), then validation errors will still be presented to the user
but valid rows will have imported.
This flag can be enabled for the model admin using the :attr:`~import_export.mixins.BaseImportMixin.skip_import_confirm`
flag.
.. _import_export_skip_admin_export_ui:
``IMPORT_EXPORT_SKIP_ADMIN_EXPORT_UI``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
A boolean value which will skip the :ref:`export form<admin_ui_exporting>` in the Admin UI, when the export is
initiated from the :ref:`change list page<admin_ui_exporting>`.
The file will be exported in a single step.
If enabled:
* the first element in the :attr:`~import_export.mixins.BaseImportExportMixin.resource_classes` list will be used.
* the first element in the :ref:`export_formats` list will be used.
This flag can be enabled for the model admin using the :attr:`~import_export.mixins.BaseExportMixin.skip_export_form`
flag.
.. _import_export_skip_admin_action_export_ui:
``IMPORT_EXPORT_SKIP_ADMIN_ACTION_EXPORT_UI``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
A boolean value which will skip the :ref:`export form<admin_ui_exporting>` in the Admin UI, but only when the export is
requested from an :ref:`Admin UI action<export_via_admin_action>`, or from the 'Export' button on the
:ref:`change form <export_from_model_change_form>`.
.. _import_export_escape_formulae_on_export:
``IMPORT_EXPORT_ESCAPE_FORMULAE_ON_EXPORT``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If set to ``True``, strings will be sanitized by removing any leading '=' character. This is to prevent execution of
Excel formulae. By default this is ``False``.
.. _import_export_escape_illegal_chars_on_export:
``IMPORT_EXPORT_ESCAPE_ILLEGAL_CHARS_ON_EXPORT``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If an export to XLSX format generates
`IllegalCharacterError <https://openpyxl.readthedocs.io/en/latest/api/openpyxl.utils.exceptions.html>`_, then
if this flag is ``True`` strings will be sanitized by removing any invalid Excel characters,
replacing them with the unicode replacement character.
By default this is ``False``, meaning that ``IllegalCharacterError`` is caught and re-raised as ``ValueError``.
.. _import_export_formats:
``IMPORT_EXPORT_FORMATS``
~~~~~~~~~~~~~~~~~~~~~~~~~
A list that defines which file formats will be allowed during imports and exports. Defaults
to ``import_export.formats.base_formats.DEFAULT_FORMATS``.
The values must be those provided in ``import_export.formats.base_formats`` e.g
.. code-block:: python
# settings.py
from import_export.formats.base_formats import XLSX
IMPORT_EXPORT_FORMATS = [XLSX]
This can be set for a specific model admin by declaring the ``formats`` attribute.
.. _import_formats:
``IMPORT_FORMATS``
~~~~~~~~~~~~~~~~~~
A list that defines which file formats will be allowed during imports. Defaults
to ``IMPORT_EXPORT_FORMATS``.
The values must be those provided in ``import_export.formats.base_formats`` e.g
.. code-block:: python
# settings.py
from import_export.formats.base_formats import CSV, XLSX
IMPORT_FORMATS = [CSV, XLSX]
This can be set for a specific model admin by declaring the ``import_formats`` attribute.
.. _export_formats:
``EXPORT_FORMATS``
~~~~~~~~~~~~~~~~~~
A list that defines which file formats will be allowed during exports. Defaults
to ``IMPORT_EXPORT_FORMATS``.
The values must be those provided in ``import_export.formats.base_formats`` e.g
.. code-block:: python
# settings.py
from import_export.formats.base_formats import XLSX
EXPORT_FORMATS = [XLSX]
This can be set for a specific model admin by declaring the ``export_formats`` attribute.
.. _exampleapp:
Example app
===========
There's an example application that showcases what import_export can do.
Before starting, set up a virtual environment ("venv") using :ref:`these instructions<create_venv>`.
You can initialize and run the example application as follows::
cd tests
./manage.py makemigrations
./manage.py migrate
./manage.py createsuperuser
./manage.py loaddata author.json category.json book.json
./manage.py runserver
Go to http://127.0.0.1:8000
For example import files, see :ref:`getting_started:Test data`.
.. _logging:
Configure logging
=================
You can adjust the log level to see output as required.
This is an example configuration to be placed in your application settings::
LOGGING = {
"version" 1,
"handlers": {
"console": {"level": "DEBUG", "class": "logging.StreamHandler"},
},
"loggers": {
"django.db.backends": {"level": "INFO", "handlers": ["console"]},
"import_export": {
"handlers": ["console"],
"level": "INFO",
},
},
}
|