Hi Russ, My goal is to load the csv file directly in the database.
So my idea is define a script containing the follow pseudo-code: while(1) and receive_csv(): value1, value2 = read_csv() MyModel.objects.create(field1=value1, field2=value2) The script run on the server, ex http://127.0.0.1:8000/admin At this point it's possible using the script reported? *import requestsurl = 'http://127.0.0.1:8000/admin'files = {'file': open('log', 'rb')}r = requests.post(url, files=files)* I don't understand where send the file respect the Django server. Thanks very much. SM Il giorno venerdì 14 febbraio 2014 00:10:44 UTC+1, Russell Keith-Magee ha scritto: > > > On Thu, Feb 13, 2014 at 4:11 PM, simone monteleone > <ares...@gmail.com<javascript:> > > wrote: > >> Hi all, >> >> I have to send a file (ex. csv file) to the Django server. >> >> I think to use the requests python module. >> >> Is this the best way? >> > > The "best" way will depend on your exact requirements - I'll come back to > this in a moment. However, Requests will certainly make it easy to write a > Python script that will send a file to a Django server. > > >> Second question; Django can receive this file? >> > > If it can be sent over HTTP, Django can receive it. > > >> And also in this case, what it is the best way? >> > > Django's file handling documentation contains details on how to set up > this sort of thing: > > https://docs.djangoproject.com/en/1.6/topics/http/file-uploads/ > > My goal is to load the csv file information in the dabatase handles by >> django, >> > > We need to be clear about your needs here. > > Do you actually need to do this over HTTP? I.e., you may want the data to > be put into your database, but is there actually a requirement to upload > that file? Could you just load the file onto the database directly? > > Django contains a bunch of tools for uploading files directly, without > going through a web interface. Look into loaddata management command for > details. > > https://docs.djangoproject.com/en/1.6/ref/django-admin/ > > There isn't a native CSV importer, but there are a couple floating around > the web that you might be able to use. > > The other approach - just write the data directly to the database. Write > some code to read the CSV file, and use Django's model objects to write the > data directly; e.g., if you've got a model called MyModel, with two fields > field1 and field2: > > MyModel.objects.create(field1=value1, field2=value2) > > will create a record in the database with those values. > > Yours, > Russ Magee %-) > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a6d9b203-8f37-4e5b-b49e-d12c96981c0a%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.