Q:

csv error while uploading file in django admin

I'm uploading a file via admin and I'm doing a small cleaning and saving the data in the Model.
So in utils I have a small function
def _map_file_to_model(row):
    _into_product = {
        "qr_id": "",
        "ean": "",
        "description": "",
        "category": "",
        "marketing_text": "",
        "bullet": "",
        "brand_image": "",
        "image": ""
    }

    return {
        _into_product[key]: value
        for key, value in row.items()
    }

this is a representation of the model. In the admin I'm doing this
@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
    change_list_template = 'product/products_blob.html'
    date_hierarchy = 'created_at'

    def get_urls(self):
        urls = super().get_urls()
        qr_urls = [
            path('import-file/', self.import_file, name='import-file'),
        ]
        return qr_urls + urls

    def import_file(self, request):
        if request.method == "POST":
            _file = request.FILES['text_file']
            reader = csv.reader(_file)

            self.message_user(request, "You're text file has been imported")
            _read_file = [_map_file_to_model(row) for row in reader]
            _product_model = _read_file
            Product.objects.bulk_create([
                Product(**data) for data in _product_model
            ])
        form = QrImportForm()
        payload = {"form": form}
        return render(
            request, 'product/text_form.html', payload
        )

and the complete Traceback
Traceback:

File "/home/copser/Documents/Project/qr-backend/venv/lib/python3.7/site-packages/django/core/handlers/exception.py" in inner
  34.             response = get_response(request)

File "/home/copser/Documents/Project/qr-backend/venv/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
  115.                 response = self.process_exception_by_middleware(e, request)

File "/home/copser/Documents/Project/qr-backend/venv/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
  113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/usr/local/lib/python3.7/contextlib.py" in inner
  74.                 return func(*args, **kwds)

File "/home/copser/Documents/Project/qr-backend/server/products/admin.py" in import_file
  36.             _read_file = [_map_file_to_model(row) for row in reader]

File "/home/copser/Documents/Project/qr-backend/server/products/admin.py" in <listcomp>
  36.             _read_file = [_map_file_to_model(row) for row in reader]

Exception Type: Error at /admin/products/product/import-file/
Exception Value: iterator should return strings, not bytes (did you open the file in text mode?)

So can someone please help me explain what is happening, I'm all ready uploading a file so no need to do whit open... but still. 

A:

Your request.FILES['text_file'] is an UploadedFile from django and not your file content. You need to call csv.reader(_file.read()) to read your uploaded file before parsing it.

