I have a problem with the django-import-export library because when I try to import excel or csv files it gives me error 500, and the error returned is the following
FileNotFoundError [Errno 2] No such file or directory: '/tmp/tmp5bcxsx9p'
and the error comes from django-import-export library in the file import_export/tmp_storages.py in _open()
def _open(self, mode="r"):
if self.name:
return open(self.get_full_path(), mode, encoding=self.encoding) <----
else:
tmp_file = tempfile.NamedTemporaryFile(delete=False)
self.name = tmp_file.name
return tmp_file
this is the code in the Django admin of the model that im trying to import info
class ProductForeignKeyWidget(ForeignKeyWidget):
def get_queryset(self, value, row, *args, **kwargs):
qs = super().get_queryset(value, row, *args, **kwargs)
return qs.filter(shop__tpc_shop=int(row['ID TIENDA']), sku=row['SKU'].upper().strip())
class StockResource(resources.ModelResource):
shop = Field(
column_name='ID TIENDA',
attribute='shop',
widget=ForeignKeyWidget(Shop, 'tpc_shop')
)
product = Field(
column_name='SKU',
attribute='product',
widget=ProductForeignKeyWidget(Product, 'sku')
)
units = Field(
column_name='UNIDADES',
attribute='units',
widget=IntegerWidget(),
default=0,
)
threshold_units = Field(
column_name='UMBRAL',
attribute='threshold_units',
widget=IntegerWidget(),
default=10,
)
location = Field(
column_name='LOCALIZACION',
attribute='location',
widget=OurCharWidget(),
default='No location',
)
@classmethod
def get_error_result_class(self):
"""
Returns the class used to store an error resulting from an import.
"""
return SimpleError
class Meta:
model = Stock
use_transactions = True
skip_unchanged = True
report_skipped = True
import_id_fields = ('product',)
fields = ('id', 'units', 'threshold_units', 'location')
def before_import_row(self, row, row_number=None, **kwargs):
row['SKU'] = str(row['SKU']).strip()
try:
shop = Shop.objects.get(tpc_shop=int(row['ID TIENDA']))
product = Product.objects.get(shop=shop, sku=row['SKU'])
stock = product.stock
old_units = stock.units
except Product.DoesNotExist:
old_units = 0
except Stock.DoesNotExist:
old_units = 0
new_units = int(row.get('UNIDADES', 0)) + old_units
row['UNIDADES'] = new_units
super().before_import_row(row, row_number=row_number, **kwargs)
def skip_row(self, instance, original, row, import_validation_errors=None):
if not instance.product:
return True
@admin.register(Stock)
class StockAdmin(ImportMixin, SimpleHistoryAdmin):
list_display = (
'id', 'tienda', linkify('product'), 'units',
'reserved_units', 'updated_at'
)
raw_id_fields = ('product',)
readonly_fields = ('units_sold', 'created_at', 'updated_at')
search_fields = ('product__sku',)
list_filter = ('product__shop__name',)
ordering = ('-updated_at',)
resource_class = StockResource
actions = [download_stock_template, remove_reserved_units, remove_all_units]
download_stock_template.short_description = 'Descargar plantilla de stock'
def tienda(self, obj):
return obj.product.shop
def get_queryset(self, request):
qs = super().get_queryset(request)
qs = qs.select_related('product__shop')
return qs
Does anyone know how I can permanently solve it? That mistake makes me tired
I tried to use CacheStorage but it doesn't work either. I don't understand why this error occurs. Sometimes it is solved when I delete the browser cookies and try to import, but it is not a definitive solution because the error appears again