Skip to content
Advertisement

How to write a django orm-like module?

I am trying to write a orm package which source data is json, First I created a Department class (I am trying to mimic django model)

JavaScript

Then I created Organization class as a search manager.

JavaScript

My expected result is:

JavaScript

But I have no idea how to achieve it, can anybody help me?

Advertisement

Answer

If you want to access the parent object instance by class attribute you have to declare it in the Department class:

JavaScript

Then in your Organization class you can write a new method that searchs for the parent data and sets the attribute before returning it:

JavaScript

Then you can access the parent attribute like you’re expecting:

JavaScript

EDIT: That snippet only gets the first parent in the hierarchy. If you want to get all children from any node then you must rewrite the _get_parent function to be recursive:

JavaScript

Now you can access all the parent objects from any node:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement