is it possible to have path string as the argument to django filefield?

714 views Asked by At

I have to create and save django model containing filefield with a downloaded attachment.

somefield = models.FileField(upload_to='somefolder/%Y/%m/%d')

I have downloaded the attachment but when I give path to the downloaded attachment as filefield upload to argument, attachment is not getting saved. I checked the folder and could not find the file.

My procedure is as follows

1.Download attachment and save it.

2.Call Model with filefield.

path='path_to_downloaded_attachment'
obj = somemodel.objects.create(param1=value, param2=value, somefield=path, param3=value)
obj.save()

Is django expecting any arguments other than path?

1

There are 1 answers

0
Árni St. Steinunnarson On

You can pass a lambda to upload_to:

def generate_filename(instance, filename):
    return find_filename(instance, filename) # based on your rules

class yourmodel(models.Model):
    somefield = models.FileField(upload_to=generate_filename)