zoukankan      html  css  js  c++  java
  • django filefield

    FileField

    class FileField(upload_to=Nonemax_length=100**options)

    A file-upload field.

    Note

    The primary_key argument isn’t supported and will raise an error if used.

    Has two optional arguments:

    FileField.upload_to

    This attribute provides a way of setting the upload directory and file name, and can be set in two ways. In both cases, the value is passed to the Storage.save() method.

    If you specify a string value or a Path, it may contain strftime() formatting, which will be replaced by the date/time of the file upload (so that uploaded files don’t fill up the given directory). For example:

    class MyModel(models.Model):
        # file will be uploaded to MEDIA_ROOT/uploads
        upload = models.FileField(upload_to='uploads/')
        # or...
        # file will be saved to MEDIA_ROOT/uploads/2015/01/30
        upload = models.FileField(upload_to='uploads/%Y/%m/%d/')
    

    If you are using the default FileSystemStorage, the string value will be appended to your MEDIA_ROOT path to form the location on the local filesystem where uploaded files will be stored. If you are using a different storage, check that storage’s documentation to see how it handles upload_to.

    upload_to may also be a callable, such as a function. This will be called to obtain the upload path, including the filename. This callable must accept two arguments and return a Unix-style path (with forward slashes) to be passed along to the storage system. The two arguments are:

    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.

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

    For example:

    def user_directory_path(instance, filename):
        # file will be uploaded to MEDIA_ROOT/user_<id>/<filename>
        return 'user_{0}/{1}'.format(instance.user.id, filename)
    
    class MyModel(models.Model):
        upload = models.FileField(upload_to=user_directory_path)
  • 相关阅读:
    Class 、NSObject、id
    xcode8.2 打包问题
    JS WEB 交互问题
    C语言 关于内存动态分配问题
    Objective-C( Category 分类,非正式协议,分类延展)
    NSComparisonResul、NSNotFound、NSEnumerationOptions......的用处
    Objective-C( Foundation框架 一 常见的结构体)
    Objective-C( Foundation框架 一 数组(NSMutableArray))
    Objective-C( Foundation框架 一 数组(NSArray))
    Objective-C( Foundation框架 一 字符串)
  • 原文地址:https://www.cnblogs.com/SunshineKimi/p/13886564.html
Copyright © 2011-2022 走看看