Questions tagged [filefield]
299 questions
78
votes
6 answers
What is the clean way to unittest FileField in django?
I have a model with a FileField. I want to unittest it. django test framework has great ways to manage database and emails. Is there something similar for FileFields?
How can I make sure that the unittests are not going to pollute the real…

luc
- 41,928
- 25
- 127
- 172
55
votes
5 answers
46
votes
4 answers
How to assign a local file to the FileField in Django?
I was trying to assign a file from my disk to the FileField, but I have this error:
AttributeError: 'str' object has no attribute 'open'
My python code:
pdfImage = FileSaver()
pdfImage.myfile.save('new', open('mytest.pdf').read())
and my…

Leodom
- 471
- 1
- 5
- 7
37
votes
11 answers
Django REST Framework and FileField absolute url
I've defined a simple Django app that includes the following model:
class Project(models.Model):
name = models.CharField(max_length=200)
thumbnail = models.FileField(upload_to='media', null=True)
(Technically yes, that could have been an…

Mark Semsel
- 7,125
- 3
- 29
- 27
35
votes
1 answer
Get absolute file path of FileField of a model instance in Django
I have a page where people can upload files to my server.
I want to do something with the file in Celery. So, I need to know the absolute filepath of the uploaded FileFiled of my Model.
Let's say I queried the model and got the instance.
Now I need…

Kotlinboy
- 3,725
- 4
- 16
- 27
33
votes
5 answers
copy file from one model to another
I have 2 simple models:
class UploadImage(models.Model):
Image = models.ImageField(upload_to="temp/")
class RealImage(models.Model):
Image = models.ImageField(upload_to="real/")
And one form
class RealImageForm(ModelForm):
class Meta:
…

Andrey
- 529
- 1
- 6
- 14
31
votes
5 answers
Django test FileField using test fixtures
I'm trying to build tests for some models that have a FileField. The model looks like this:
class SolutionFile(models.Model):
'''
A file from a solution.
'''
solution = models.ForeignKey(Solution)
file =…

sttwister
- 2,279
- 1
- 19
- 23
19
votes
2 answers
how to download a filefield file in django view
I have filefield in my django model:
file=models.FileField(verbose_name=u'附件',upload_to='attachment/%Y/%m/%d',max_length=480)
This file will display in the web page with link…

Angelia
- 333
- 1
- 3
- 9
17
votes
3 answers
How to show download link for attached file in FileField in Django Admin?
I have FileField in my django model:
file = models.FileField(upload_to=FOLDER_FILES_PATH)
In Django admin section for changing this model I have full path to this file (by default):
Currently:…

SkyFox
- 1,805
- 4
- 22
- 33
14
votes
6 answers
How to mix Django, Uploadify, and S3Boto Storage Backend?
Background
I'm doing fairly big file uploads on Django. File size is generally 10MB-100MB.
I'm on Heroku and I've been hitting the request timeout of 30 seconds.
The Beginning
In order to get around the limit, Heroku's recommendation is to upload…

David Kay
- 1,006
- 10
- 16
14
votes
1 answer
Django model with FileField -- dynamic 'upload_to' argument
I am using the model with FileField to deal with file uploading. Now the files can be uploaded successfully. However, there is one more small improvement I want to make, which is to create folder for the user with the username.
Here is the code I've…

Mona
- 1,425
- 6
- 21
- 31
13
votes
3 answers
Django dynamic FileField upload_to
I'm trying to make dynamic upload path to FileField model. So when user uploads a file, Django stores it to my computer /media/(username)/(path_to_a_file)/(filename).
E.g. /media/Michael/Homeworks/Math/Week_1/questions.pdf or…

Jertzuuka
- 163
- 1
- 1
- 5
13
votes
6 answers
Renaming files in Django FileField
I know that there is a very similar thread here, but I cannot find the solution to my problem.
I need to rename a file which is save in django models.FileField
I tried this
os.rename(old_path, new_path)
mod.direct_file =…

aemdy
- 3,702
- 6
- 34
- 49
12
votes
4 answers
How to Email a Django FileField as an Attachment?
I need to send a models.FileField as an email attachment using Django. I've seen snippets that show how to do this with the raw request.FILES data (which still contains the Content-Type), but have not been able to find anything that shows how to do…

Lyle Pratt
- 5,636
- 4
- 27
- 28
12
votes
4 answers
How to update a file location in a FileField?
I have a FileField in a model. For each instance of the model, I would like that the filename on the disk stays updated with the value of another field (let's call it label) of the model.
At the moment, I use a custom upload_to() function that…

mimo
- 2,469
- 2
- 28
- 49