Upload file to a dynamically generated location

135 views
Skip to first unread message

pieceof...@gmail.com

unread,
Jan 8, 2018, 3:34:32 PM1/8/18
to Django users
Hey Friends,

I'm having trouble figuring out how to upload a document model to a dynamically generated location.  The following models.py file contains two models and a helper function


views.py


# I would like the string inputs to be self.bundle.user, and self.bundle.name  where self is the document model.  I know this doesn't work which is why I am here asking this question.
def get_user_document_directory():
    document_directory
= 'media/{0}/{1}/documents/'.format( ____, _____ )
   
return document_directory

class Bundle(models.Model):
    user
= models.ForeignKey(User)
    name
= models.CharField(max_length=100)

class Document(models.Model):
    bundle
= models.ForeignKey(Bundle)
    name
= models.CharField(max_length=100)
    description
= models.CharField(max_length=100)
    document
= models.FileField(upload_to=get_user_document_directory(self) ) # I know passing self doesn't work but just trying to get the point across of what I am trying to do.
    uploaded_at
= models.DateTimeField(auto_now_add=True)



Any comments about how to remedy this situation would be greatly appreciated. 

Thanks,
K

Andréas Kühne

unread,
Jan 9, 2018, 2:16:52 AM1/9/18
to django...@googlegroups.com
Hi,

If you look at the documentation for the upload_to parameter, you can see that you can do exactly what you want:

The upload_to method (in your case "get_user_document_directory") can take 2 parameters:
ArgumentDescription
instance

An instance of the model where the FileField is defined. More specifically, this is the particular instance where the current file is being attached.

In most cases, this object will not have been saved to the database yet, so if it uses the default AutoFieldit might not yet have a value for its primary key field.

filenameThe filename that was originally given to the file. This may or may not be taken into account when determining the final destination path.

So you don't need to add "self", you get that automatically. All you need to do is change the method to:

def get_user_document_directory(instance, filename):
    return "media/{0}/{1}/documents/{2}".format(instance.bundle.user, instance.bundle.name, filename)

You have to remember to include the filename as well (it's not only the directory but the entire path).

Regards,

Andréas

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/71125202-9b89-4572-a086-ab9fed2b4d01%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages