Home > OS >  Access "upload_to" of a Model's FileFIeld in Django?
Access "upload_to" of a Model's FileFIeld in Django?

Time:05-29

I have a Model with a FileField like that:

class Video(MediaFile):
    """ Model to store Videos """
    file = FileField(upload_to="videos/")
    [...]

I'm populating the DB using a cron script.

Is it possible to somehow access the "upload_to" value of the model? I could use a constant, but that seems messy. Is there any way to access it directly?

CodePudding user response:

You can access this with:

Video.file.field.upload_to  # 'videos/'

or through the _meta object:

Video._meta.get_field('file').upload_to  # 'videos/'

The upload_to=… parameter [Django-doc] can however also be given a function that takes two parameters, and thus in that case it will not return a string, but a reference to that function.

  • Related