Skip to content

Usage problem: can't delay configuration until after model class declaration. #22

@oO

Description

@oO

I'm having a problem figuring out where/when to call minimongo.configure() in my Pyramid app, to make sure that my minimongo class actually have a valid connection to the database.

The whole metaclass thing is causing the connection to be created when the module is imported and the class declaration is parsed, which is way before I've had a chance to read my setting files and get valid values for host and database.

It seems that I can't write an example like this in a single file:

import minimongo


class MyModel( minimongo.Model ):
    """Base MiniMongo model. all models should inherit from this one"""
    class Meta:
        """Collection definition should go here."""
        colection = "test"


def main():
    minimongo.configure(
        host = "mongo.ozoux.com",
        database = "test"
    )
    a = MyModel()
    a.name = "foo"
    a.save()


if __name__ == '__main__':
    main()

I get the following error:

 File "test_minimongo.py", line 21, in <module> 
 class MyModel( minimongo.Model ):
 File "minimongo/minimongo/model.py", line 49, in __new__ 
 (name, options.host, options.port, options.database)
 Exception: Model 'MyModel' improperly configured: localhost 27017 None

How exactly can I use minimongo with a deferred configuration call? I'd like to be able to define my models wherever I want, and as long as the call to configure happens before the class is instanciated, everything should be fine, no?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions