gpt4 book ai didi

python - Django 文件上传和模型 FileField

转载 作者:太空狗 更新时间:2023-10-30 00:18:17 27 4
gpt4 key购买 nike

我太接近了……但我不太清楚从上传 View 到模型的联系。当我在模型的 FileField 中使用回调时,上传工作正常,但我不确定实际文件复制发生在哪里。目标是确保 chunking 正在发生,但文件复制操作似乎隐藏在某处?

这是我所拥有的:

型号:

def get_media_upload_dir(instance, filename):
user_id = instance.user.id
upload_dir = "%s/%d/%s" % (settings.MEDIA_ROOT, user_id, filename)
print "Upload dir set to: %s" % upload_dir
return upload_dir

class MediaFile(models.Model):
media_file = models.FileField(upload_to=get_media_upload_dir)
download_count = models.PositiveIntegerField(default=0)

查看:

def file_upload(request, course_id):    
if request.method == 'POST':
form = FileUploadForm(request.POST, request.FILES)
if form.is_valid():
uploaded = form.cleaned_data['file_upload']
mediaFile = MediaFile(media_file=uploaded,
owner=request.user.profile,
creator=request.user.profile)
mediaFile.save()

return HttpResponseRedirect('/course/%s/' % course_id)
else:
form = FileUploadForm()
return render_to_response('course/file_upload.html', {'form':form,'course':course}, context_instance=RequestContext(request))

最佳答案

存储发生在这里:http://code.djangoproject.com/browser/django/trunk/django/db/models/fields/files.py#L90 . Django 使用它自己的 API 来访问文件存储:http://docs.djangoproject.com/en/dev/ref/files/storage/ .但是,如果分块是您所需要的,那么您可以采用 Bartek 的建议!

关于python - Django 文件上传和模型 FileField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3181574/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com