usually you put the DBSession and such somewhere other than models/__init__.py,
like models/meta.py. That way sub-modules and packages of "models" can import
the dependencies from meta.py.
structure is like:
models/__init__.py:
from .model1 import Foo, Bar
from .model2 import Bat, Hoho
models/meta.py:
Base = declarative_base()
models/model1/__init__.py:
from .foo import Foo
from .bar import Bar
models/model1/foo.py:
from ..meta import Base
class Foo(Base):
# ...
models/model1/bar.py:
from ..meta import Base
class Bar(Base):
# ...
models/model2/__init__.py:
from .bat import Bat
from .hoho import Hoho
models/model2/bat.py:
from ..meta import Base
class Bat(Base):
# ...
models/model2/hoho.py:
from ..meta import Base
class Hoho(Base):
# ...
On Aug 1, 2012, at 2:38 PM, John Anderson wrote:
> Do you have an example of an app doing this?
>
> For instance, if your models/__init__.py declares your DBSession and your
> Base:
>
> # __init__.py
> DBSession = scoped_session(session_maker())
>
> class BaseModel(object):
> pass
>
> Base = declarative_base(cls=BaseModel)
>
> you would just import everything after those? i.e
>
> from app.module1.models import *
> from app.module2.models import *
>
>
> then then inside app/module1/__init__.py you would do the imports for all of
> its models?
>
> # app/module1/__init__.py
> from app.module1.modules import *
>
> # app/module1/models.py
> from app.models import Base
>
> class Model1(Base):
> pass
>
> or would you structure it differently? Wouldn't you have issues with
> __init__.py importing the modules but the modules needing Base from
> __init__.py?
>
> On Wednesday, August 1, 2012 11:30:43 AM UTC-5, Michael Bayer wrote:
> usually the pattern is you just import one module, like "from myapp import
> model". "model/__init__.py" then has imports for everything within, and the
> pattern repeats as you descend through the directory tree, that is, every top
> level __init__.py imports the important bits from within that package.
>
> the only other way would be to do a find of .py files within a model
> directory and then import them with importlib or similar, which is more
> complicated, non-deterministic as far as ordering and more prone to import
> resolution issues. Adding per-package imports as you go along just creates
> this same traversal as part of the code.
>
> if the Python interpreter is never told of the existence of some .py file, it
> doesn't exist. it doesn't matter that "Base" is used in that file, the
> classes within don't exist until their owning module is imported.
>
>
>
> On Aug 1, 2012, at 12:07 PM, John Anderson wrote:
>
>> In my pyramid apps I have a create script that generates my database for
>> production and a different script that generates my database for my tests.
>>
>> But if I don't import the module the models are in then they aren't
>> discovered even though they all share the same Base which is the class I get
>> the metadata from to call create_all, like:
>>
>> Base.metadata.create_all(engine)
>>
>> If I import them then they are picked up but I don't want to have to
>> remember to import every new module I create. Is there a good way to do
>> model discovery for this?
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "sqlalchemy" group.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msg/sqlalchemy/-/r95QN9vJ_IgJ.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected].
>> For more options, visit this group at
>> http://groups.google.com/group/sqlalchemy?hl=en.
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/sqlalchemy/-/7VlbuxrkgBkJ.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/sqlalchemy?hl=en.
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.