Django:任何更改“upload_to”的方法FileField的属性不用诉诸魔法? [英] Django: Any way to change "upload_to" property of FileField without resorting to magic?

查看:487
本文介绍了Django:任何更改“upload_to”的方法FileField的属性不用诉诸魔法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看此博客发布 ...很老,所以也许事情已经改变了。但在我的实验中他们没有。为了动态更改模型字段FileField upload_to 路径,您必须使用信号并创建自定义模型字段。讨厌。我不能想象有动态上传路径是一个特殊的用例,它不符合标准的Django框架?我错过了什么吗?有没有办法完成这个?

See this blog post... It's quite old so maybe things have changed. But in my experimenting they have not. In order to change the model field FileField upload_to path dynamically, you must resort to using signals and creating custom model fields. Nasty. I can't imagine that having a dynamic upload path is such a special use case that it's not addressed by the standard Django framework? Am I missing something? Is there any other way to accomplish this?

本质上我想这样做:

def MyModel(models.Model):
    fileUpload = models.FileField(upload_to='media/', null=True, blank=True)

def save(self, **kwargs):
    # Retrieve the user's id/pk from their profile
    up = UserProfile.objects.get(email=self.email)

    # All their uploads go into their own directory
    self.file_image.upload_to = up.id

    super(MyModel, self).save()

然而,在我尝试的10种不同的实现中,Django讨厌所有这些。特别是对于这个文件,该文件被上传到默认路径'media /'

However, in the 10 different implementations I tried, Django hates all of them. For this one in particular, the file is uploaded to the default path 'media/'.

尝试对参数绘制一个模型,并将这些参数传递给一个dict对象,创建一个MyModel对象,设置MyModel.fileUpload.upload_to参数,然后将该dict复制到模型中并保存。不行。

I've tried scraping a modelform for parameters and passing those parameters into a dict object, create a MyModel object, set the MyModel.fileUpload.upload_to parameter, then copy the dict into the model and save. Doesn't work.

我也试图覆盖 __ init __ 方法,但猜测是什么?那么在对象创建中这么早,它实际上并没有定义 self.email !所以这不行。

I also tried to override the __init__ method, but guess what? That is so early in the object creation that it doesn't actually have self.email defined yet! So that doesn't work.

任何想法或必须遵循原始链接中概述的奥秘解决方案?

Any ideas or must I follow the arcane solution outlined in the original link?

推荐答案

因此,在 FileField'字段中,实际上有一个相当简单的解决方案, upload_to 关键字参数实际上可以作为一个参数。您在 upload_to` kwarg中指定的功能应该具有此签名。

So there is actually a fairly easy solution to this, in the FileField' field, theupload_tokeyword argument can actually take a function as a parameter. The function that you specify in yourupload_to` kwarg should have this signature.

def my_awesome_upload_function(instance, filename):
    """ this function has to return the location to upload the file """

     return os.path.join('/media/%s/' % instance.id, filename)

在这种情况下,实例是具有FileField的模型实例, filename是上传文件的文件名。所以你的模型就像上面的例子,如下所示:

In this case, instance is the instance of your model that has the FileField, and filename is the filename of the uploaded file. So your model like in your example above would look like this:

def MyModel(models.Model):
    fileUpload = models.FileField(upload_to=my_awesome_upload_function, null=True, blank=True)

如果这是有道理的您可以根据已上传的文件的模型实例和文件名,更改 my_awesome_upload_function 以生成您根据您的偏好上传文件的路径。

If this makes sense you can now change my_awesome_upload_function to generate you path to upload the file to based on your preference, given the model instance and filename of the file that has been uploaded.

这篇关于Django:任何更改“upload_to”的方法FileField的属性不用诉诸魔法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆