首页 > django > Manager从文件而不是从数据库中提供查询集

Manager从文件而不是从数据库中提供查询集 (Manager gives a queryset from files and not from a database)

问题

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

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

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

我的梦想可能吗?

解决方法

我会通过将对象重命名为_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/领域)。

问题

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?

解决方法

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).

相似信息