I define some Entities which works fine; for meta programming issues. I now need to reflect the field properties defined in the model.
For example:
class Foo(Entity):
bar = OneToMany('Bar')
baz = ManyToMany('Baz')
Which type of relation is set: "ManyToMany", "OneToMany" or even a plain "Field", and the relation target?
Is there any simple way to reflect the Elixir Entities?
You can do introspection in Elixir as you would anywhere in Python -- get all names of attributes of class Foo
with dir(Foo)
, extract an attribute given its name with getattr(Foo, thename)
, check the type of the attribute with type(theattr)
or isinstance
, etc. The string 'Bar'
that you pass as the attribute to the constructor of any Relationship
subclass (including OneToMany
and ManyToMany
) ends up as the r.of_kind
attribute of the resulting instance r of the Relationship subclass.
Module inspect
in the Python standard library may be a friendlier way to do introspection, but dir / getattr / isinstance &c are perfectly acceptable in many cases.