Django import export не работает

Django import-export — позволяет пользователям импортировать данные

Я пытаюсь использовать импорт-экспорт django, чтобы пользователи могли импортировать свои собственные данные. Я интегрировал его с администратором, и он работает хорошо, но мне не удается заставить работать пользовательский импорт.

Вот мои взгляды:

Моя модель Word:

И мой шаблон import.html:

Как ни странно, сначала казалось, что это работает, когда я пытался с файлом csv, но не с файлом xlsx. Затем я внес изменения только в xlsx-часть моего кода. Это не только не устранило проблему, но и теперь csv не работает.

Обратите внимание: это не из-за столбца идентификатора, так как в моих тестовых файлах загрузки есть пустой столбец идентификатора.

Обновление . Я понял, что мне не хватает поля пользователя, которое является внешним (не нулевым) ключом в моей модели Word. Итак, я добавил ForeignKeyWidget, но получаю следующую ошибку: NOT NULL constraint failed: vocab_word.user_id . Ошибка сохраняется, даже если я добавлю столбец с идентификатором пользователя. Как я могу это исправить?

Мой CSV-файл выглядит так:

Отслеживание после внедрения изменений Мэтью:

1 ответ

Ваша ошибка совершенно ясна — объект не может быть создан, потому что поле user_id имеет значение NULL во время создания:

Ваш csv содержит:

Ваша модель Word также определяет поле пользователя:

Это означает, что когда вы объявляете Resource в django-import-export, вам необходимо указать, как csv username может быть сопоставлен с любыми существующими экземплярами user через отношение FK.

Вы должны использовать для этого ForeignKeyWidget , потому что он обрабатывает сопоставление полей csv с объектами.

  • column_name определяет столбец csv, который мы используем для поиска пользовательских ссылок
  • attribute определяет атрибут модели Word , который должен быть установлен

Кроме того, мы должны убедиться, что отношения пользователей ищутся с использованием «правильного» поля. Из документов:

В поле поиска по умолчанию используется первичный ключ (pk) в качестве критерия поиска, но его можно настроить для использования любого поля в связанной модели.

Собирая все вместе, наше определение Field выглядит так:

Поэтому я думаю, что причиной вашей ошибки было то, что вы неправильно установили attribute на username . Если вы отлаживаете код, вы, вероятно, обнаружите, что экземпляр ‘testuser’ User был загружен и назначен на Word.username , который будет просто проигнорирован, а Word.user будет иметь значение null, отсюда и ошибка.

Обновить

Еще одна проблема, которую нужно исправить:

Объявление fields должно ссылаться на атрибуты модели, которые должны быть установлены из данных csv.

Следовательно, поле username должно быть user , потому что это атрибут модели, который необходимо обновить.

Источник

Installation and configurationВ¶

django-import-export is available on the Python Package Index (PyPI), so it can be installed with standard Python tools like pip or easy_install :

This will automatically install many formats supported by tablib. If you need additional formats like cli or Pandas DataFrame , you should install the appropriate tablib dependencies (e.g. pip install tablib[pandas] ). Read more on the tablib format documentation page.

Alternatively, you can install the git repository directly to obtain the development version:

Читайте также:  Приложение вызов не работает

Now, you’re good to go, unless you want to use django-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.

All prerequisites are set up! See Getting started to learn how to use django-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 False . 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. 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 В¶

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_IMPORT_PERMISSION_CODE В¶

If set, lists the permission code that is required for users to perform the “import” action. Defaults to None , which means everybody can perform imports.

Django’s built-in permissions have the codes add , change , delete , and view . You can also add your own permissions.

IMPORT_EXPORT_EXPORT_PERMISSION_CODE В¶

If set, lists the permission code that is required for users to perform the “export” action. Defaults to None , which means everybody can perform exports.

Django’s built-in permissions have the codes add , change , delete , and view . You can also add your own permissions.

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.

Example appВ¶

There’s an example application that showcases what django-import-export can do. It’s assumed that you have set up a Python venv with all required dependencies or are otherwise able to run Django locally.

You can run it via:

books-sample.csv contains sample book data which can be imported.

© Copyright 2012–2020, Bojan Mihelac Revision 722c376e .

Источник

Getting startedВ¶

For example purposes, we’ll use a simplified book app. Here is our models.py :

Creating import-export resourceВ¶

To integrate django-import-export with our Book model, we will create a ModelResource class in admin.py that will describe how this resource can be imported or exported:

Exporting dataВ¶

Now that we have defined a ModelResource class, we can export books:

Customize resource optionsВ¶

By default ModelResource introspects model fields and creates Field -attributes with an appropriate Widget for each field.

To affect which model fields will be included in an import-export resource, use the fields option to whitelist fields:

Or the exclude option to blacklist fields:

An explicit order for exporting fields can be set using the export_order option:

The default field for object identification is id , you can optionally set which fields are used as the id when importing:

When defining ModelResource fields it is possible to follow model relationships:

Читайте также:  Как настроить магнитную антенну для рации самому без прибора

Following relationship fields sets field as readonly, meaning this field will be skipped when importing data.

By default all records will be imported, even if no changes are detected. This can be changed setting the skip_unchanged option. Also, the report_skipped option controls whether skipped records appear in the import Result object, and if using the admin whether skipped records will show in the import preview page:

Declaring fieldsВ¶

It is possible to override a resource field to change some of its options:

Other fields that don’t exist in the target model may be added:

Fields Available field types and options.

Advanced data manipulation on exportВ¶

Not all data can be easily extracted from an object/model attribute. In order to turn complicated data model into a (generally simpler) processed data structure on export, dehydrate_ method should be defined:

In this case, the export looks like this:

Customize widgetsВ¶

A ModelResource creates a field with a default widget for a given field type. If the widget should be initialized with different arguments, set the widgets dict.

In this example widget, the published field is overridden to use a different date format. This format will be used both for importing and exporting resource.

Widgets available widget types and options.

Importing dataВ¶

Let’s import some data!

In the fourth line we use modelresource_factory() to create a default ModelResource . The ModelResource class created this way is equal to the one shown in the example in section Creating import-export resource .

In fifth line a Dataset with columns id and name , and one book entry, are created. A field for a primary key field (in this case, id ) always needs to be present.

In the rest of the code we first pretend to import data using import_data() and dry_run set, then check for any errors and actually import data this time.

Import data workflow for a detailed description of the import workflow and its customization options.

Deleting dataВ¶

To delete objects during import, implement the for_delete() method on your Resource class.

The following is an example resource which expects a delete field in the dataset. An import using this resource will delete model instances for rows that have their column delete set to 1 :

SignalsВ¶

To hook in the import export workflow, you can connect to post_import , post_export signals:

Admin integrationВ¶

ExportingВ¶

Exporting via list filtersВ¶

Admin integration is achieved by subclassing ImportExportModelAdmin or one of the available mixins ( ImportMixin , ExportMixin , ImportExportMixin ):

A screenshot of the change view with Import and Export buttons.

A screenshot of the import view.

A screenshot of the confirm import view.

Exporting via admin actionВ¶

Another approach to exporting data is by subclassing ImportExportActionModelAdmin which implements export as an admin action. As a result it’s possible to export a list of objects selected on the change list page:

A screenshot of the change view with Import and Export as an admin action.

Note that to use the ExportMixin or ExportActionMixin , you must declare this mixin before admin.ModelAdmin :

Note that ExportActionMixin is declared first in the example above!

ImportingВ¶

It is also possible to enable data import via standard Django admin interface. To do this subclass ImportExportModelAdmin or use one of the available mixins, i.e. ImportMixin , or ImportExportMixin . Customizations are, of course, possible.

Customize admin import formsВ¶

It is possible to modify default import forms used in the model admin. For example, to add an additional field in the import form, subclass and extend the ImportForm (note that you may want to also consider ConfirmImportForm as importing is a two-step process).

Читайте также:  Люстра что может сломаться

To use the customized form(s), overload ImportMixin respective methods, i.e. get_import_form() , and also get_confirm_import_form() if need be.

For example, imagine you want to import books for a specific author. You can extend the import forms to include author field to select the author from.

To further customize admin imports, consider modifying the following ImportMixin methods: get_form_kwargs() , get_import_resource_kwargs() , get_import_data_kwargs() .

Using the above methods it is possible to customize import form initialization as well as importing customizations.

Admin available mixins and options.

© Copyright 2012–2020, Bojan Mihelac Revision 1ef3252e .

Источник

Django Import Export — UNIQUE constraint failed

I am following this documentation on how to use django-import-export:

I have an excel sheet that looks like below

I want to store the data in this model:

This is how far I got:

This is what I get:

Here is the CSV File:

When I try to import multiple rows like this:

I get this error:

Here is the CSV file:

Thank you for any suggestions

1 Answer 1

The way import works is the following:

  • You need a unique identifier so that when importing, the import-export module «knows» whether it needs to create a new row or modify an existing row.
  • If that unique identifier is not the model’s id field (which in your case is the auto-incremented row number), then it must be some other unique field. None of the fields var1 . var4 on your model are declared as unique, so as you’ve defined your model currently, it’s impossible to import.
  • You can use multiple «id fields» if a combination of fields is unique together, but that’s also not the case on your model.
  • The import module with actually do a get_or_create() call on your database, using the unique_id_fields as the get parameters. This should never return more than 1 row, if it were to return multiple rows, the import would crash.

To give you an idea, and assuming SAP_ID is a unique identifier (I doubt Max. Capacity is, the name doesn’t indicate it would be), this should be your model:

Note your model also has the implicit field id , which import-export knows about and you have to decide what to do with it, since it’s not in your import data. This is why you’re receiving the error you mentioned, because it’s a field in your model that you haven’t assigned to anything. Best is to exclude it, since it’s not relevant here (Django will auto-increment a new id if the row doesn’t exist yet, like when you create the model with ExcelData.objects.create() ):

With this code and the following csv file, it works, I’ve tested it:

When trying to import a second time or if there’s already some data in the database where the value of var2 is d, e or f, you’ll get UNIQUE constraint failed errors for exceldata.var2 .

Note: If Max. Capa really is your unique id field, then you shouldn’t assign it to var4 but to id . That would also work, although as I mentioned above I doubt that’s your desired behaviour.

Источник

Оцените статью