How to create relations dynamically

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How to create relations dynamically

Babroo
Hi, I'm wondering if the following is possible:

I have a list of existing object properties and I would like to dynamically create several relations between two instances. The only way that I found is with python_name, but the problem is that also creating python_name dynamically is not possible. do you have any solution for this?


obj_prop = ['has_for_ingredient', 'has_side_efect', ...]

with onto:
    class Drug(Thing):  pass

my_drug = Drug("my_drug")
my_drug2 = Drug("my_drug2")

for prop in obj_prop :

     prop_entity= onto[prop]
     prop_entity.python_name = 'temp'

     my_drug .temp = [my_drug2]   # this does not work, only first relation will be created.




Thanks

Reply | Threaded
Open this post in threaded view
|

Re: How to create relations dynamically

Jiba
Administrator
Hi,

You can do:

     setattr(my_drug, prop_name, [my_drug2])


or:

     prop_entity= onto[prop]
     prop_entity[my_drug] = [my_drug2]

Best regards,
Jiba
Reply | Threaded
Open this post in threaded view
|

Re: How to create relations dynamically

Babroo
Thank you for your reply.