model.FileField

39 views
Skip to first unread message

mangu rajpurohit

unread,
Apr 27, 2015, 6:14:24 AM4/27/15
to django...@googlegroups.com
Hi,

I am new to django. I am looking for examples for uploading/downloading files in django. I found that models.FileField is used in model. So, my question is that, is it possible to upload file in django, without requiring to create a model, which will keep track of filenames in database.

James Schneider

unread,
Apr 27, 2015, 6:27:41 AM4/27/15
to django...@googlegroups.com

Have you seen this?

https://docs.djangoproject.com/en/1.8/topics/http/file-uploads/

Has an example showing an example exactly as you asked without any models involved. Also make sure you recognize the difference between models.FileField and forms.FileField:

https://docs.djangoproject.com/en/1.8/ref/models/fields/#filefield
https://docs.djangoproject.com/en/1.8/ref/forms/fields/#filefield

The former is for definitions in models and the corresponding database column, and the latter is for display within web forms.

The overlap for naming between forms and models has burned me more than once.

-James

On Apr 27, 2015 3:14 AM, "mangu rajpurohit" <rajpuro...@gmail.com> wrote:
Hi,

I am new to django. I am looking for examples for uploading/downloading files in django. I found that models.FileField is used in model. So, my question is that, is it possible to upload file in django, without requiring to create a model, which will keep track of filenames in database.

--
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...@googlegroups.com.
To post to this group, send email to django...@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/16371334-b89d-4385-b141-0eb75c1056ec%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

mangu rajpurohit

unread,
Apr 27, 2015, 9:17:39 AM4/27/15
to django...@googlegroups.com
Hi James,

Thanks for suggesting me the page at location  https://docs.djangoproject.com/en/1.8/topics/http/file-uploads/. I visited that page and as you suggested, there was no need of database in between, However, I noted that we have to manually write the file as shown in snippet given below(copied from url suggested by you)

def handle_uploaded_file(f):
    with open('some/file/name.txt', 'wb+') as destination:
        for chunk in f.chunks():
            destination.write(chunk)

So, I mean, Isn't there any way through which this file writing operation takes places automatically in django. Moreover, you note that here, filepath is hard-coded. I want that it file uploaded by user, must be copied at MEDIA_ROOT variable set in settings.py of project.
 

James Schneider

unread,
Apr 27, 2015, 1:17:23 PM4/27/15
to django...@googlegroups.com

You can save the file wherever you like, just change the file path where you are writing the file to make use of settings.MEDIA_ROOT rather than a hard-coded URL.

Since you aren't using a model, Django is assuming you want to do something fancy with the file, so you are responsible for doing the heavy lifting for operations such as writing it to disk.

One or more of these packages may also help:

https://www.djangopackages.com/grids/g/file-managers/

-James

--
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...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages