管理器提供来自文件而不是数据库的查询集

发布于 2024-09-15 13:56:01 字数 332 浏览 5 评论 0原文

我想重写管理器类,以便允许将内容数据从文本文件(此处为“/one directory/myPrefix_*”)加载到内容字段中,而不是从数据库表中加载。

class Things(model.Models):
    file    = CharField(max_length = 25, primary key = True)
    content = TextField()
    objects = myManager("/one directory/", "myPrefix_")

如果可能的话,我很乐意在管理站点中使用此类。

我的疯狂梦想可能实现吗?

I would like to override the manager class in order to allow content data to be loaded from text files (here on of "/one directory/myPrefix_*") into content field instead of from a database table.

class Things(model.Models):
    file    = CharField(max_length = 25, primary key = True)
    content = TextField()
    objects = myManager("/one directory/", "myPrefix_")

I would appreciate to use this class in the admin site, if possible.

Is my wild dream possible?

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

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

发布评论

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

评论(1

懒猫 2024-09-22 13:56:02

我将通过将对象重命名为 _objects,然后创建一个属性函数来加载数据来解决这个问题。类似下面的代码应该可以满足您的需要...

class Things(model.Models):
    file    = CharField(max_length = 25, primary key = True)
    content = TextField()
    _objects = CharField(max_length = 50)

    @property
    def objects(self):
        return load_content_from("/one directory/myPrefix_" + self._objects)

如果您想查看管理站点中文件的内容,那么您可能需要创建自己的字段类型(请参阅 http://code.djangoproject.com/browser/django/trunk/django/db/models/fields )。

I would solve this by renaming objects to _objects, and then creating a property function to load the data. Something like the code below should do what you need...

class Things(model.Models):
    file    = CharField(max_length = 25, primary key = True)
    content = TextField()
    _objects = CharField(max_length = 50)

    @property
    def objects(self):
        return load_content_from("/one directory/myPrefix_" + self._objects)

If you want to see the content from the files in the admin site then you'll probably need to create your own field type (see http://code.djangoproject.com/browser/django/trunk/django/db/models/fields).

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