动态upload_to无法正常工作

时间:2018-07-09 11:33:06

标签: python django django-models django-views django-file-upload

我正在尝试使用python遍历我的硬盘并将文件分别存储在数据库中。上传文件时,我的动态upload_to工作正常,但是当我尝试使用编写的算法通过硬盘驱动器循环保存文件时,它不起作用(upload_to)。有什么帮助吗?

我将在下面添加代码:

models.py

def upload_path(instance, filename):
    return os.path.join('file', instance.folder, filename)

类文件(models.Model):

#Defintion to set upload_to path


file_title = models.CharField(max_length=255)
file_type = models.CharField(max_length=255)
folder = models.CharField(max_length=255)
document = models.FileField(upload_to=upload_path)
uploaded_at = models.DateTimeField(auto_now_add=True)

def __unicode__(self):
    return self.file_title

def delete(self):
    delete_path = os.path.join('file', self.document.path)

    os.remove(delete_path)
    return super(File,self).delete()

views.py(摘要)-遍历硬盘驱动器并保存到数据库的算法

for sfile in os.listdir(secondary_directory):
            if os.path.isfile(secondary_directory + sfile):

                #Extract File Information
                filename, filetype = os.path.splitext(sfile)
                folder = cfile

                if not exists(filename, 'file'):
                    #Work with file info
                    x = File()
                    x.file_title = filename
                    x.file_type = filetype
                    x.folder = folder
                    x.document = sfile

                    x.save()

循环算法:

def SyncDriveToDatabase(request):

HttpResponse(request, 'brick/loading.html')

#############################
#File Folder
#############################

primary_directory = os.path.join(root, 'file/')

if not os.path.exists(primary_directory):
    os.makedirs(primary_directory)


for cfile in os.listdir(primary_directory):
    if os.path.isfile(primary_directory + cfile):


        #Extract File Information
        filename, filetype = os.path.splitext(cfile)
        folder = 'Main'

        if not exists(filename, 'file'):
            #Work with file info
            x = File()
            x.file_title = filename
            x.file_type = filetype
            x.folder = folder
            x.document = cfile

            x.save()



    elif os.path.isdir(primary_directory + cfile):
        secondary_directory = os.path.join(primary_directory, cfile) + '/'

        for sfile in os.listdir(secondary_directory):
            if os.path.isfile(secondary_directory + sfile):

                #Extract File Information
                filename, filetype = os.path.splitext(sfile)
                folder = cfile

                if not exists(filename, 'file'):
                    #Work with file info
                    x = File()
                    x.file_title = filename
                    x.file_type = filetype
                    x.folder = folder
                    x.document = sfile

                    x.save()

##################################
#Audio Folder
##################################

primary_directory = os.path.join(root, 'audio/')

if not os.path.exists(primary_directory):
    os.makedirs(primary_directory)

#Loop through primary_directory
for cfile in os.listdir(primary_directory):

    if os.path.isfile(primary_directory + cfile):

        #Extract File information
        filename, filetype = os.path.splitext(cfile)
        album = 'Unkown'
        artist = 'Unkown'

        if not exists(filename, 'audio'):
            a = Audio()
            a.audio_title = filename
            a.audio_type = filetype
            a.album = album
            a.artist = artist
            a.document = cfile
            a.save()



    elif os.path.isdir(primary_directory + cfile):
        artist_directory = os.path.join(primary_directory, cfile) + '/'

        for sfile in os.listdir(artist_directory):

            if os.path.isfile(sfile):

                #Extract File information
                filename, filetype = os.path.splitext(sfile)
                album = 'Unkown'
                artist = cfile
                if not exists(filename, 'audio'):
                    #Work with file info
                    a = Audio()
                    a.audio_title = filename
                    a.audio_type = filetype
                    a.album = album
                    a.artist = artist
                    a.document = sfile
                    a.save()

            elif os.path.isdir(artist_directory + sfile):
                album_directory = os.path.join(artist_directory, sfile) + '/'

                for tfile in os.listdir(album_directory):

                    if os.path.isfile(album_directory + tfile):

                        #Extract File information
                        filename, filetype = os.path.splitext(tfile)
                        album = sfile
                        artist = cfile

                        if not exists(filename, 'audio'):
                            #Work with file info
                            a = Audio()
                            a.audio_title = filename
                            a.audio_type = filetype
                            a.album = album
                            a.artist = artist
                            a.document = tfile
                            a.save()







######################
#Image Folder
#######################

primary_directory = os.path.join(root, 'image/')

if not os.path.exists(primary_directory):
    os.makedirs(primary_directory)


for cfile in os.listdir(primary_directory):
    if os.path.isfile(primary_directory + cfile):

        #Extract File Information
        filename, filetype = os.path.splitext(cfile)
        folder = 'Main'

        if not exists(filename, 'image'):
            #Work with file info
            i = Image()
            i.image_title = filename
            i.folder = folder
            i.document = cfile
            i.save()

    elif os.path.isdir(primary_directory + cfile):

        secondary_directory = os.path.join(primary_directory, cfile) + '/'

        for sfile in os.listdir(secondary_directory):
            if os.path.isfile(secondary_directory + sfile):

                #Extract File Information
                filename, filetype = os.path.splitext(sfile)
                folder = cfile
                if not exists(filename, 'image'):
                    #Work with file info
                    i = Image()
                    i.image_title = filename
                    i.folder = folder
                    i.document = sfile
                    i.save()

######################
#Video Folder
#######################

primary_directory = os.path.join(root, 'video/')

if not os.path.exists(primary_directory):
    os.makedirs(primary_directory)

for cfile in os.listdir(primary_directory):

    if os.path.isfile(primary_directory + cfile):

        #Extract File Information
        filename, filetype = os.path.splitext(cfile)
        folder = 'Main'

        if not exists(filename, 'video'):
            #Work with file info
            v = Video()
            v.folder = folder
            v.video_title = filename
            v.document = cfile
            v.save()

    elif os.path.isdir(primary_directory + cfile):

        secondary_directory = os.path.join(primary_directory, cfile) + '/'

        for sfile in os.listdir(secondary_directory):
            if os.path.isfile(secondary_directory + sfile):

                #Extract File Information
                filename, filetype = os.path.splitext(sfile)
                folder = cfile

                if not exists(filename, 'video'):
                    #Work with file info
                    v = Video()
                    v.folder = folder
                    v.video_title = filename
                    v.document = sfile
                    v.save()





return redirect('index')

1 个答案:

答案 0 :(得分:0)

我使用Daniel的输入解决了我的问题。我更改了文档的值,它开始起作用。请参阅下面的新代码:

旧代码:

...
x.document = sfile

x.save()

新代码:

x.document = os.path.join('file', folder, sfile)

x.save()