简体   繁体   English

从FileSystemStorage访问模型

[英]Accessing model from FileSystemStorage

I have custom FileSystemStorage. 我有自定义FileSystemStorage。
The idea is to pass optional filename parameter. 这个想法是传递可选的文件名参数。
My custome storge code: 我的客户存储代码:

class SalesMapFileStores(FileSystemStorage):
    def __init__(self, location=None, base_url=None, filename=None):
        if filename:
            self.filename = filename
        super(SalesMapFileStores, self).__init__(location=location, base_url=base_url)

    def get_available_name(self, name):
        return name

    def get_valid_name(self, name):
        if self.filename:
            return self.filename
        return name

    def _save(self, name, content):
        if self.exists(name):
            self.delete(name)
        return super(SalesMapFileStores, self)._save(name, conten

What i whant is to pass this filename parameter from model. 我想从模型中传递此文件名参数。
Somethin like this: 像这样的东西:

class SalesMapImage(models.Model):
    name = models.CharField(max_length=254, verbose_name='Filename')
    image = SalesMapImageField(upload_to='SalesMap/Test', storage=SalesMapFileStores(filename=name), verbose_name='Test Image',
                               content_types=('image/jpeg', 'image/png'))

But in this case, Django passes as a parameter itself model.CharField (it's obvious :)). 但是在这种情况下,Django将参数model.CharField传递给它(很明显:))。
The question is: how can I get access to my model instance from Storage? 问题是: 如何从存储访问我的模型实例?

Thanks in advance! 提前致谢!

Well, it is a bit crazy idea but you can try to override the assignment to that field in that class so the SalesMapFileStores instance always keep in sync with the name field like this: 好吧,这是个疯狂的主意,但是您可以尝试覆盖该类中对该字段的分配,因此SalesMapFileStores实例始终与name字段保持同步,如下所示:

class SalesMapImage(models.Model):
    name = models.CharField(max_length=254, verbose_name='Filename')
    image = SalesMapImageField(upload_to='SalesMap/Test', storage=SalesMapFileStores(), verbose_name='Test Image',
                               content_types=('image/jpeg', 'image/png'))

    def __setattr__(self, key, value):
        self.__dict__[key]=value
        if key=='name':
            self.image.storage.filename = value

And the general idea is to hook the assignment of the value to the update of the filename field. 通常的想法是将值的分配与文件名字段的更新挂钩。

This is supposing that you don't want to update it manually from within your view. 假设您不想从视图中手动更新它。 Because it wouldn't take much effort to do model_instance.storage.filename = self.name or even add a method to your custom storage class to update the filename. 因为执行model_instance.storage.filename = self.name甚至不需要在您的自定义存储类中添加方法来更新文件名就不需要太多的工作。

UPDATE: Heads up for the storage=SalesMapFileStores() . 更新:前往storage=SalesMapFileStores() There you are passing a instance of SalesMapFileStores . 在那里,您传递了SalesMapFileStores的实例。 Not the class, so it might be possible you'll be using the same instace for storing all files and this my bring conflicts with filename . 不是类,因此您可能会使用相同的实例来存储所有文件,这会与filename产生冲突。 You can try it like this: storage=SalesMapFileStores 您可以这样尝试: storage=SalesMapFileStores

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM