Jaymon / prom

A PostgreSQL or SQLite orm for Python
MIT License
22 stars 4 forks source link

Can we magically map children to parent? #164

Closed Jaymon closed 1 year ago

Jaymon commented 1 year ago
class Foo(Orm): pass

class Bar(Orm):
    foo_id = Field(Foo)

So if I have a Foo instance, could I do something like:


f = Foo.create()
b = Bar.create(foo_id=f.pk)
print(b.pk) # 1

print(f.bar.pk) # 1

Basically, the __getattr__ algorithm would check Foo, if it couldn't find anything, then it would check the internal list of Orm children and see if something matches .model_name, and then it would see if that matching Bar class has a Field instance referencing Foo, then it would query using .one().

If you did f.bars then it would check .models_name and call .get().