简体   繁体   中英

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

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM