Import csv data into database in Django Admin

后端 未结 3 1580
粉色の甜心
粉色の甜心 2020-12-04 08:51

I\'ve tried to import a csv file into a database by tweaking the modelform inside the admin doing this:

models.py:

class Data(models.Model):
    plac         


        
3条回答
  •  醉梦人生
    2020-12-04 09:14

    In the save() method, you don't have any access to the request object - you can see that it's not passed in. Normally you would expect to have a NameError there, but I suspect that you've got a function elsewhere in the file called request().

    At the point of saving, all the relevant data should be in cleaned_data: so you should be able to do

    file_csv = self.cleaned_data['file_to_import']
    

    At that point you'll have another problem, which is when you get to open - you can't do that, as file_to_import is not a file on the server filesystem, it's an in-memory file that has been streamed from the client. You should be able to pass file_csv directly to csv.reader.

提交回复
热议问题