Hi Yap,

Thanks for that full addressing trick.  I've confirmed it works in the
scope of a turbogears project, but as Ben Bangert wrote in the post
you linked:
> This works, though its rather verbose

So full addressing is a solution better than anything else discussed
here, but it's awkward and I feel like Elixir should do a better job
at knowing the right thing.

For reference, the whole metaclass issue with Yap's solution looks
like this:
{23981776:
   {'Dog': <class 'tgmultifilemodel.submodels.animals.Dog'>,
    'Entity': <class 'elixir.entity.Entity'>},
 22361496:
   {'Entity': <class 'elixir.entity.Entity'>,
    'Man': <class 'tgmultifilemodel.submodels.humans.Man'>}
}
so it looks like we still have the same initial problem described
above (the different files' Entities are in different 'domains'), but
it works because they're fully addressed.  I guess this is how you
make the metaclass issue work, but again, I feel like Elixir should
know how to do this without the code being so annoyingly verbose.


For more reference, the final solution I used (as a turbogears
project) looks like this:
tgmultifilemodel/
   controllers.py
   model.py
   submodels/
      __init__.py
      animals.py
      humans.py


model.py
--------------
from elixir import setup_all
from submodels import *

## Debugging Outputs
print "Man's metaclass' entities: "
print Man.__metaclass__._entities

print "Dog's metaclass' entities: "
print Dog.__metaclass__._entities   #Same as Man's metaclass


setup_all()


submodels/__init__.py:
--------------
from animals  import *
from humans   import *


submodels/animals.py
--------------
from elixir import Field, Unicode, ManyToOne, Entity
#from tgmultifilemodel.model import Man  #Won't work: Circular
imports!

class Dog(Entity):

    name = Field(Unicode(10))
    owner = ManyToOne('tgmultifilemodel.model.Man')
    # NOTE: You could have done
    #  owner = ManyToOne('tgmultifilemodel.submodels.humans.Man')
    # but the above works due to the way we structured model.py and is
slightly less verbose

    def __repr__(self):
        return '<Dog \'%s\'>' % (self.name)



submodels/humans.py
--------------
from elixir import Field, Unicode, OneToMany, Entity
#from tgmultifilemodel.model import Dog  #Won't work: Circular
imports!

class Man(Entity):

    name = Field(Unicode(10))
    pets = OneToMany('tgmultifilemodel.model.Dog')

    def __repr__(self):
        return '<Man \'%s\'>' % (self.name)


controllers.py
--------------
....
from tgmultifilemodel.model import *

class Root(controllers.RootController):
   ...


I'll update the Elixir wiki in the next few days to include this
solution.
If Elixir is ever updated in the future to make this issue less
awkward, please make a note of it in this thread.


-Dan


On Apr 30, 4:56 am, Yap Sok Ann <[EMAIL PROTECTED]> wrote:
> On Apr 30, 11:32 am, Yap Sok Ann <[EMAIL PROTECTED]> wrote:
>
>
>
> > The model below doesn't seem to work with any of the import methods
> > laid out in this discussion and in the wiki. It has an "odd many-to-
> > many relationship" which is modeled following Gaeten's suggestion 
> > inhttp://www.mail-archive.com/[EMAIL PROTECTED]/msg02616.html
>
> > man.py
> > ------
> > from elixir import Entity, Field, Unicode, has_many
>
> > class Man(Entity):
> >     name = Field(Unicode(10))
> >     has_many('relationships', of_kind='Relationship')
> >     def __repr__(self):
> >         return '<Man \'%s\'>' % (self.name)
>
> > woman.py
> > --------
> > from elixir import Entity, Field, Unicode, has_many
>
> > class Woman(Entity):
> >     name = Field(Unicode(10))
> >     has_many('relationships', of_kind='Relationship')
> >     def __repr__(self):
> >         return '<Woman \'%s\'>' % (self.name)
>
> > relationship.py
> > ---------------
> > from elixir import Entity, Field, Unicode, belongs_to
>
> > class Relationship(Entity):
> >     name = Field(Unicode(10))
> >     belongs_to('man', of_kind='Man', required=True)
> >     belongs_to('woman', of_kind='Woman', required=True)
> >     def __repr__(self):
> >         return '<Relationship \'%s\'>' % (self.name)
>
> > Tested under Elixir 0.5.2
>
> I got it to work using full path addressing as mentioned by Gaeten 
> inhttp://groups.google.com/group​/sqlelixir/browse_thread/thread​/bc8b23a1ae8bd9ec
>
> Dan, can you try if it works for you? Should work fine regardless of
> how to structure the files or where to do the import.
>
> FWIW, here's what I have:
>
> model/__init__.py
> -----------------
>
> model/man.py
> ------------
> from elixir import Entity, Field, Unicode, has_many
>
> c​lass Man(Entity):
>     name = Field(Unicode(10))
>     has_many('re​lationships', of_kind='model.relationship.Re
> lationship')
>     has_many('women', through='relationships', via='woman')
>     def __repr__(self):
>         return '<Man \'%s\'>' % (self.name)
>
> model/woman.py
> ------​--------
> from elixir import Entity, Field, Unicode, has_many
>
> class Woman(Entity):
>     name = Field(Unicode(10))
>     has_many('relationships',
> of_kind='model.relationship.Relationship')
>     has_many('men', through='relationships', via='man')
>     def __repr__(self):
>         return '<Woman \'%s\'>' % (self.name)
>
> model/relationship.py
> ---------------------
> from elixir import Entity, Field, Unicode, belongs_to
>
> class Relationship(Entity):
>     name = Field(Unicode(10))
>     belongs_to('man', of_kind='model.man.Man',
> inverse='relationships', required=True)
>     belongs_to('woman', of_kind='model.woman.Woman',
> inverse='relationships', required=True)
>     def __repr__(self):
>         return '<Relationship \'%s\'>' % (self.name)
>
> test.py
> -------
> from elixir import Entity, setup_all
>
> from model.man import Man
> from model.woman import Woman
> from model.relationship import Relationship
>
> print Entity.__metaclass__._entities
>
> setup_all()
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"SQLElixir" 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/sqlelixir?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to