views:

24

answers:

1

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?

A: 

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

Andrew Wilkinson
First, thanks for your answer.But in this case, Thinks.objects.all() would not be accessible anymore because the manager would have been replaced. I guess 'content' should rather be given by a callable @property. 'content' is a read_only property, and a new field representing the file name is added.Much simpler than a custom manager!
MUY Belgium