I would like my file upload to be renamed before being saved to disk. The logic I want to use to set the name would be contained in the view.
I want to pass my getname variable from my view into my model class before saving. Here is my broken code.
View:
getname = 'foo' #I want this string to get passed into upload_to in the model
form = DocumentForm(request.POST, request.FILES)
if form.is_valid():
newdoc = Document()
newdoc.filename = getname #This line doesn't work
newdoc.docfile = request.FILES['docfile']
newdoc.save()
Model:
class Document(models.Model):
filename = ''
docfile = models.FileField(upload_to=filename)
I have read through several of these links but whenever I touch my code I break it.
http://www.malloc.co/django/django-rename-a-file-uploaded-by-a-user-before-saving/ http://catherinetenajeros.blogspot.com/2013/03/rename-path-when-imagefile-upload.html
Edit: I've revised the code to help make my question more clear.