On 5/14/07, RonnyPfannschmidt <[EMAIL PROTECTED]> wrote:
>
> usualy explicit backreferences for has_a/belongs_to/has_many/.. are
> fine
>
> but in some case like plugins that add new entities it is usefull to
> have them automated since one cant rely in the fact that those plugins
> will be availiable in every case
>
> usage/example:
>
> auth.py
>
> class User(Entity):
> has_auto_backreferences()
> has_field('username', Unicode(30))
>
>
> plugins/auth/roles.py
>
> class Role(Entity):
> has_field('name')
> has_and_bolongs_to_many('users', of_kind='User', inverse='roles')
> # will automatically add the fitting relation to the User entity
>
> having auto-backreferences would help with DRY and allow to write
> modular application without having dependency problems cause of
> disabled plugins
The above example should already work:
> auth.py
>
> class User(Entity):
> has_field('username', Unicode(30))
>
>
> plugins/auth/roles.py
>
> class Role(Entity):
> has_field('name')
> has_and_belongs_to_many('users', of_kind='User', backref='roles')
Notice the word "backref" instead of "inverse". And besides, it
shouldn't be necessary to add the "has_auto_backreferences" statement
in the other entity.
The ugly case is the following, which will not work (and I'm unsure if
we should make it work):
class User(Entity):
has_field('username', Unicode(30))
class Role(Entity):
has_field('name')
# this won't work because it needs a foreign key from user to Role.
has_many('users', of_kind='User', backref='role')
But for that scenario, the following should work, provided you use delay_setup:
> auth.py
class User(Entity):
has_field('username', Unicode(30))
then...
> plugins/auth/roles.py
class UserWithRole(User):
belongs_to('role', of_kind='Role')
setattr(auth, 'User', UserWithRole)
class Role(Entity):
has_field('name')
has_many('users', of_kind='User', inverse='role')
--
Gaƫtan de Menten
http://openhex.org
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---