我有一个这样的模型
def upload_location(instance, filename): return 'validate/%s/builds/%s' % (get_current_user(), filename) class MidletPair(models.Model): jad_file = models.FileField(upload_to = upload_location) jar_file = models.FileField(upload_to = upload_location) upload_to=tempfile.gettempdir()
如何在 upload_location()... 中获取当前用户?
旁注:查找 django 的东西很困惑,因为网上有很多 pre-1.0 的东西。
最佳答案
当前用户存储在请求对象中,除非您从其他地方传入它,否则您无法在模型方法中获取它 - 您不能在 upload_to 函数中执行此操作。
因此您需要以不同的方式处理此问题 - 我建议在表单级别进行处理。您可以将请求对象传递到表单的 __init__
方法并将其存储在实例属性中,您可以在自定义上传处理程序中访问它。有关上传处理程序的文档,请查看 here .
关于python - 如何将 django FileField 保存到用户文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/858213/