简体   繁体   中英

Django import-export library ForeignKey error (IntegrityError: FOREIGN KEY constraint failed in django)

First of all please sorry for my English. ))

So I have a problem with Django import-export library when I try to import data from csv/xls/xlsx files to the Django application DB.

How it looks like.

Here is my models.py:

class Department(models.Model):
    department_name = models.CharField(max_length = 50, default = '')
    def __str__(self):
        return f'{self.department_name}'

class ITHardware(models.Model):
    it_hardware_model = models.CharField(max_length = 100)
    it_hardware_serial_number = models.CharField(max_length = 100, 
        blank = True, default = '')
    it_hardware_department = models.ForeignKey(Department, 
        related_name = 'department', on_delete = models.SET_NULL, default = '', 
        null = True, blank = True, db_constraint=False)

admin.py:

@admin.register(Department)
class DepartmentAdmin(admin.ModelAdmin):
    list_display = ('department_name', )
    actions = [duplicate_object]

@admin.register(ITHardwareManufacturer)
class ITHardwareManufacturerAdmin(admin.ModelAdmin):
    list_display = ('manufacturer', )
    actions = [duplicate_object]

class ITHardwareImportExportAdmin(ImportExportModelAdmin):
    resource_class = ITHardwareResource
    list_display = ['id', 'it_hardware_manufacturer', 
        'it_hardware_model', 'it_hardware_serial_number', 
        'it_hardware_department']
    actions = [duplicate_object]

resource.py:

class ITHardwareResource(resources.ModelResource):
    it_hardware_department = fields.Field(
        column_name = 'it_hardware_department',
        attribute = 'ITHardware.it_hardware_department',
        widget = widgets.ForeignKeyWidget(Department, field = 'department_name'))
    
    class Meta():
        model = ITHardware
        fields = (
            'id',
            'it_hardware_model', 
            'it_hardware_serial_number', 
            'it_hardware_department', 
            )
        export_order = (
            'id',
            'it_hardware_model', 
            'it_hardware_serial_number', 
            'it_hardware_department', 
            )

import file:

导入文件

If I try to import data from file, I get such error:

String number: 1 - ITHardwareManufacturer matching query does not exist.
None, Canon, BX500CI, 5B1837T00976, Office_1, IT_1

And so on.

OK. When I fill Department table manually via adminpanel (pointed there IT_1, 2, 3 and 4 departments) I get this preview:

预习

and then I get this error:

错误

Please explain me what am I doing wrong and how to fix it.

Update

If I understood the concept correctly, then "column_name" is the name of the column from which/to which data is imported/exported, and "attribute" is the name of the database field for the same operations.

The export works fine for me. However, for import, as I understand it, I incorrectly describe the mechanism for ForeignKey. That is, I'm trying to tell Django that when importing data from the "it_hardware_department" column of the file, this data should be written to the "it_hardware_department" field of the Department model using ForeignKeyWidget.

Perhaps some intermediate action is required to determine the mechanism for writing data to the Department model, where to specify something like "Department__it_hardware_department"?

Update: traceback

Traceback (most recent call last):
  File "C:\users\iv\mip\mip_env\lib\site-packages\django\db\backends\base\base.py", line 242, in _commit
    return self.connection.commit()

The above exception (FOREIGN KEY constraint failed) was the direct cause of the following exception:
  File "C:\users\iv\mip\mip_env\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\users\iv\mip\mip_env\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\users\iv\mip\mip_env\lib\site-packages\django\utils\decorators.py", line 130, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "C:\users\iv\mip\mip_env\lib\site-packages\django\views\decorators\cache.py", line 44, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "C:\users\iv\mip\mip_env\lib\site-packages\django\contrib\admin\sites.py", line 232, in inner
    return view(request, *args, **kwargs)
  File "C:\users\iv\mip\mip_env\lib\site-packages\django\utils\decorators.py", line 43, in _wrapper
    return bound_method(*args, **kwargs)
  File "C:\users\iv\mip\mip_env\lib\site-packages\django\views\decorators\http.py", line 40, in inner
    return func(request, *args, **kwargs)
  File "C:\users\iv\mip\mip_env\lib\site-packages\import_export\admin.py", line 113, in process_import
    result = self.process_dataset(dataset, confirm_form, request, *args, **kwargs)
  File "C:\users\iv\mip\mip_env\lib\site-packages\import_export\admin.py", line 125, in process_dataset
    return resource.import_data(dataset,
  File "C:\users\iv\mip\mip_env\lib\site-packages\import_export\resources.py", line 771, in import_data
    return self.import_data_inner(
  File "C:\users\iv\mip\mip_env\lib\site-packages\import_export\utils.py", line 25, in __exit__
    self.context_manager.__exit__(*args)
  File "C:\users\iv\mip\mip_env\lib\site-packages\django\db\transaction.py", line 246, in __exit__
    connection.commit()
  File "C:\users\iv\mip\mip_env\lib\site-packages\django\utils\asyncio.py", line 33, in inner
    return func(*args, **kwargs)
  File "C:\users\iv\mip\mip_env\lib\site-packages\django\db\backends\base\base.py", line 266, in commit
    self._commit()
  File "C:\users\iv\mip\mip_env\lib\site-packages\django\db\backends\base\base.py", line 242, in _commit
    return self.connection.commit()
  File "C:\users\iv\mip\mip_env\lib\site-packages\django\db\utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\users\iv\mip\mip_env\lib\site-packages\django\db\backends\base\base.py", line 242, in _commit
    return self.connection.commit()

Exception Type: IntegrityError at /admin/mip_apps/ithardware/process_import/
Exception Value: FOREIGN KEY constraint failed

Based on your code sample, the import process will look for a field called it_hardware_department in the import file (and we can see that it is there). During import, it will attempt to find the FK relation in Department by doing a simple get(name=<value in column> . It will store this value in ITHardware.it_hardware_department - however this won't work because this isn't a valid model field. You should rename this to it_hardware_department (which you said you did). The fact that shows up in the preview screen tells me that this worked ok.

The 'FK contraint' failed error says that you have tried to write to the table with an invalid FK relationship. I suggest run the debugger or add a print to inspect the object before it is saved. It is likely that one of your required Foreign Keys will be empty here. I don't think it is it_hardware_department because this has a value in the preview. Are there other FKs in your model which are not listed?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM