Discussion:
[Modeling-users] creating objects by name
John Lenton
2004-07-08 01:38:08 UTC
Permalink
I'm certain this has been asked and answered before, but I can't find
it anywhere: how can I create an object by name? By this I mean,
instead of doing

from MyModel.Thing import Thing
a_thing = Thing()
ec.insert(a_thing)

what magic do I use to do

do_magic()
a_thing = more_magic('Thing')
ec.insert(a_thing)

?
--
John Lenton (***@gmail.com) -- Random fortune:
bash: fortune: command not found
Sebastien Bigaret
2004-07-08 04:28:05 UTC
Permalink
I don't remember that this was asked before, anyhow here is the magic :)

(provided that the model has already been loaded into the
defaultModelSet)
Post by John Lenton
import AuthorBooks
from Modeling import ClassDescription
cd=ClassDescription.classDescriptionForName('Book')
# get the class
... cd.classForInstances()
<class 'Book.Book'>
Post by John Lenton
from Modeling.EditingContext import EditingContext
ec=EditingContext()
# create an instance
... book=cd.createInstanceWithEditingContext(ec)
<Book.Book object at 0x4103158c>
Post by John Lenton
# WARNING! it is NOT registered inside the EC
... print book.editingContext()
None

2 remarks:

- createInstanceWithEditingContext() completely ignores the EC, the
object still need to be inserted into the EC afterwards. I need to
double-check this carefully (esp. wrt DBChannel.fetchObject() if
you're curious). Plus the documentation is incorrect, part of it
applies to classForInstances() --esp. for the delegate part)

- the real stuff (importing the correct module and class) is done by
EntityClassDescription.classForEntity()


-- Sébastien.
Post by John Lenton
I'm certain this has been asked and answered before, but I can't find
it anywhere: how can I create an object by name? By this I mean,
instead of doing
from MyModel.Thing import Thing
a_thing = Thing()
ec.insert(a_thing)
what magic do I use to do
do_magic()
a_thing = more_magic('Thing')
ec.insert(a_thing)
?
--
bash: fortune: command not found
Loading...