django 自定义FileField upload_to上传路径

def user_directory_path(instance, name):
    """
    clean_data内容:
    for k ,v in clean_data:
    K: file
    V: record1301DL00220230602全部.txt
    K: name
    V: record1301DL00220230602全部.txt
    """
    filename = name[15:23]
    # return the whole path to the file
    return "{0}/{1}".format(filename, name)


class Fujian(models.Model):
    name = models.CharField(max_length=32, verbose_name='文件名称')
    # file = models.FileField(upload_to='%Y%m%d')
    file = models.FileField(upload_to=user_directory_path)
    uploaded_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = '上传文件信息'

 

posted @ 2023-06-04 15:55  笑而不语心自闲  阅读(67)  评论(0编辑  收藏  举报