django-photologue upload_to

发布于 2024-09-16 15:18:48 字数 255 浏览 5 评论 0原文

我已经使用 django-photologue 一段时间了,发现它是所有其他图像处理应用程序的绝佳替代品。

但有一件事是,我还使用 django-cumulus 来将上传内容推送到 CDN,而不是在本地计算机/服务器上运行。

当我使用 imagekit 时,我总是可以传递 upload_to='whatever' 但我似乎无法使用 photologue 执行此操作,因为它会自动插入图像字段。我将如何实现某种覆盖?

问候

I have been playing around with django-photologue for a while, and find this a great alternative to all other image handlings apps out there.

One thing though, I also use django-cumulus to push my uploads to my CDN instead of running it on my local machine / server.

When I used imagekit, I could always pass a upload_to='whatever' but I cannot seem to do this with photologue as it automatically inserts the imagefield. How would I go about achieving some sort of an overwrite?

Regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

安静 2024-09-23 15:18:48

我认为您可以在将实例保存到数据库之前连接到照片模型的 pre_save 信号,并更改 upload_to 字段。

看看这个:
http://docs.djangoproject.com/en/dev/topics/signals/< /a>

I think you can hook into the pre_save signal of the Photo model, and change the upload_to field, just before the instance is saved to the database.

Take a look at this:
http://docs.djangoproject.com/en/dev/topics/signals/

凉城已无爱 2024-09-23 15:18:48

设法找到解决方法,但这需要您在 photologue/models.py 中进行更改

if PHOTOLOGUE_PATH is not None:
    if callable(PHOTOLOGUE_PATH):
        get_storage_path = PHOTOLOGUE_PATH
    else:
        parts = PHOTOLOGUE_PATH.split('.')
        module_name = '.'.join(parts[:-1])
        module = __import__(module_name)
        get_storage_path = getattr(module, parts[-1])
else:
    def get_storage_path(instance, filename):
        dirname = 'photos'
        if hasattr(instance, 'upload_to'):
            if callable(instance.upload_to):
                dirname = instance.upload_to()
            else: dirname = instance.upload_to
        return os.path.join(PHOTOLOGUE_DIR, 
                        os.path.normpath( force_unicode(datetime.now().strftime(smart_str(dirname))) ), 
                        filename)

,然后在您的应用程序模型中执行以下操作:

class MyModel(ImageModel):
    text = ...
    name = ...

    def upload_to(self):
        return 'yourdirectorynamehere'

Managed to find a workaround for it, however this requires you to make the changes in photologue/models.py

if PHOTOLOGUE_PATH is not None:
    if callable(PHOTOLOGUE_PATH):
        get_storage_path = PHOTOLOGUE_PATH
    else:
        parts = PHOTOLOGUE_PATH.split('.')
        module_name = '.'.join(parts[:-1])
        module = __import__(module_name)
        get_storage_path = getattr(module, parts[-1])
else:
    def get_storage_path(instance, filename):
        dirname = 'photos'
        if hasattr(instance, 'upload_to'):
            if callable(instance.upload_to):
                dirname = instance.upload_to()
            else: dirname = instance.upload_to
        return os.path.join(PHOTOLOGUE_DIR, 
                        os.path.normpath( force_unicode(datetime.now().strftime(smart_str(dirname))) ), 
                        filename)

And then in your apps models do the following:

class MyModel(ImageModel):
    text = ...
    name = ...

    def upload_to(self):
        return 'yourdirectorynamehere'
大海や 2024-09-23 15:18:48

您可以使用设置 PHOTOLOGUE_PATH 来提供您自己的可调用项。定义一个以实例和文件名作为参数的方法,然后返回您想要的任何内容。例如,在您的settings.py中:

import photologue

...

def PHOTOLOGUE_PATH(instance, filename):
  folder = 'myphotos' # Add your logic here
  return os.path.join(photologue.models.PHOTOLOGUE_DIR, folder, filename)

大概(虽然我没有测试过这一点)您可以找到有关 Photo 实例的更多信息(例如它涉及的其他实例)并相应地选择文件夹。

You can use the setting PHOTOLOGUE_PATH to provide your own callable. Define a method which takes instance and filename as parameters then return whatever you want. For example in your settings.py:

import photologue

...

def PHOTOLOGUE_PATH(instance, filename):
  folder = 'myphotos' # Add your logic here
  return os.path.join(photologue.models.PHOTOLOGUE_DIR, folder, filename)

Presumably (although I've not tested this) you could find out more about the Photo instance (e.g. what other instances it relates to) and choose the folder accordingly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文