Thread: [Modeling-users] self.getId()
Status: Abandoned
Brought to you by:
sbigaret
From: <so...@la...> - 2003-03-17 19:00:39
|
Hy all, so this is for big take a look at http://larsen-b.com As you can see, i got modeling working well :) I get one question: how could i get the id for an object (I ever see this on the list, but i miss it). cause while working on web using real objects (read between two transaction) is impossible without using user session, so i think working on id is a nice way to solve this for me. I know that working on id is a bad habit but .. Should i use something like ? ================================================================= class Article(CustomObject): [snip of generated] def getID(self): self.editingContext().globalIDForObject(o).keyValues()['id'] ================================================================= |
From: <so...@la...> - 2003-03-17 22:07:08
|
On Mon, Mar 17, 2003 at 07:24:04PM +0100, so...@la... wrote: > > > I get one question: > how could i get the id for an object (I ever see this > on the list, but i miss it). cause while working on > web using real objects (read between two transaction) > is impossible without using user session, so i think > working on id is a nice way to solve this for me. > I know that working on id is a bad habit but .. Answering myself. The doc says : "you shouldn't need to expose the PK values as class' attributes. But, ok, if you really want to do that, that is to say, if you declare them as class properties, " That 's what i did, but this is wrong too, let read this snapshot: =========================================================== from mortal.Objects.Article import Article from Modeling.EditingContext import EditingContext from Modeling.FetchSpecification import FetchSpecification # creating a new object ec = EditingContext() article = Article() article.setText('Oups') article.setTitle('Again') ec.insertObject(article) ec.saveChanges() # fetching the last inserted fetchSpec=FetchSpecification(entityName='Article') ec.objectsWithFetchSpecification(fetchSpec)[-1] <mortal.Objects.Article.Article instance at 0x85cfd44> ec.objectsWithFetchSpecification(fetchSpec)[-1].getId() # <---> the id is 0 cause the fetch isn't done here. 0 ec.dispose() # <--> after a dispose it's work . ec.objectsWithFetchSpecification(fetchSpec)[-1].getId() 26L ========================================================== 1) first i don't want to dispose each time i insert a new object cause this can really be time consumming 2) for an unknow reason right now, (perhaps i'll debug that) even calling dispose() on my ec don't force a re-fecht in a real application So do you have any idea on how shoud i try ? |
From: Sebastien B. <sbi...@us...> - 2003-03-17 23:33:12
|
so...@la... wrote: > On Mon, Mar 17, 2003 at 07:24:04PM +0100, so...@la... wrote: > >=20 > >=20 > > I get one question:=20 > > how could i get the id for an object (I ever see this=20 > > on the list, but i miss it). cause while working on=20 > > web using real objects (read between two transaction) > > is impossible without using user session, so i think=20 > > working on id is a nice way to solve this for me.=20 > > I know that working on id is a bad habit but ..=20 >=20 > Answering myself. The doc says : > "you shouldn't need to expose the PK values as class' attributes. But, > ok, if you really want to do that, that is to say, if you declare them > as class properties, "=20 >=20 > That 's what i did, but this is wrong too, let read this=20 > snapshot: >=20 > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > from mortal.Objects.Article import Article > from Modeling.EditingContext import EditingContext > from Modeling.FetchSpecification import FetchSpecification >=20 > # creating a new object > ec =3D EditingContext() > article =3D Article() > article.setText('Oups') > article.setTitle('Again') > ec.insertObject(article) > ec.saveChanges() >=20 > # fetching the last inserted=20 > fetchSpec=3DFetchSpecification(entityName=3D'Article') > ec.objectsWithFetchSpecification(fetchSpec)[-1] > <mortal.Objects.Article.Article instance at 0x85cfd44> > ec.objectsWithFetchSpecification(fetchSpec)[-1].getId() > # <---> the id is 0 cause the fetch isn't done here. > 0=20 > ec.dispose() > # <--> after a dispose it's work .=20 > ec.objectsWithFetchSpecification(fetchSpec)[-1].getId() > 26L > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D That is really strange. I just made the very same thing with one of the framework's testPackage and everything seems all right: >>> from Modeling.EditingContext import EditingContext >>> from Modeling.FetchSpecification import FetchSpecification >>> from testPackages.AuthorBooks.Book import Book >>> b=3DBook() >>> b.setTitle('blah') >>> ec=3DEditingContext() >>> ec.insertObject(b) >>> ec.saveChanges() >>> ec.globalIDForObject(b).keyValues()['id'] 63L ...wait... Oh, ok, now I see: >>> from Modeling.EditingContext import EditingContext >>> from Modeling.FetchSpecification import FetchSpecification >>> from testPackages.AuthorBooks.Book import Book >>> b=3DBook() >>> b.setTitle('blah') >>> ec=3DEditingContext() >>> ec.insertObject(b) >>> ec.saveChanges() >>> ec.globalIDForObject(b).keyValues()['id'] 64L >>> b._id 0 You are using getId() (returning article._id) instead of getID() (returning the PK value stored in its GlobalID) ! --> using getID() will probably make it. However, we have a bug here: Article's PK did not get its value. > 2) for an unknow reason right now, (perhaps i'll debug that)=20 > even calling dispose() on my ec don't force a re-fecht > in a real application I must check that, this is not a normal behaviour at all. article._id should have received its value. I'll check that. Seems you find one again ;) -- S=E9bastien. |
From: <so...@la...> - 2003-03-17 23:49:58
|
On Tue, Mar 18, 2003 at 12:33:32AM +0000, Sebastien Bigaret wrote: > > so...@la... wrote: > > On Mon, Mar 17, 2003 at 07:24:04PM +0100, so...@la... wrote: > > > That is really strange. I just made the very same thing with one of the > framework's testPackage and everything seems all right: > > >>> from Modeling.EditingContext import EditingContext > >>> from Modeling.FetchSpecification import FetchSpecification > >>> from testPackages.AuthorBooks.Book import Book > >>> b=Book() > >>> b.setTitle('blah') > >>> ec=EditingContext() > >>> ec.insertObject(b) > >>> ec.saveChanges() > >>> ec.globalIDForObject(b).keyValues()['id'] > 63L > > > ...wait... Oh, ok, now I see: > > >>> from Modeling.EditingContext import EditingContext > >>> from Modeling.FetchSpecification import FetchSpecification > >>> from testPackages.AuthorBooks.Book import Book > >>> b=Book() > >>> b.setTitle('blah') > >>> ec=EditingContext() > >>> ec.insertObject(b) > >>> ec.saveChanges() > >>> ec.globalIDForObject(b).keyValues()['id'] > 64L > >>> b._id > 0 > > You are using getId() (returning article._id) instead of getID() > (returning the PK value stored in its GlobalID) ! > > > However, we have a bug here: Article's PK did not get its value. Yeah you got it :) If don't use the id as classProperty as said in the doc, and use the getID() helper sent before on the list it works well :) I guess I win one more point in my debugging level :) |
From: Sebastien B. <sbi...@us...> - 2003-03-18 01:22:49
|
[snip] > >=20 > > You are using getId() (returning article._id) instead of getID() > > (returning the PK value stored in its GlobalID) ! > >=20 > >=20 > > However, we have a bug here: Article's PK did not get its value. >=20 >=20 > Yeah you got it :)=20 > If don't use the id as classProperty as said in the doc, and use=20 > the getID() helper sent before on the list it works well :)=20 I included a small patch at the end of the message to solve this issue --I'll make the unittest tomorrow and will correct this on the cvs rep. Now article._id should get its value as expected after ec.saveChanges(), if it's set as a class property. > I guess I win one more point in my debugging level :)=20 . The Evil Bug is struck down by a Mighty Patch=20 . You find an Inquisition Helmet +22 in the Evil Bug's treasure room ;) Thanks for the report, hopefully everything is ok now. BTW and back to your original question, why don't you use GlobalIDs rather than raw id number? This needs some pre- and post-processing if you need to pass them as strings in a http POST e.g., but you'll gain a generic scheme then. I remember using this transformation you might find handy: KeyGlobalID -> '<entityName>#<pk1name>#<pk1value>[#...]' and back (splitting with '#') i.e. something like that: def gid_to_str(gid): if gid.isTemporary(): raise ValueError, 'TemporaryGlobalIDs are not supported' gid_str=3Dgid.entityName() for pk,value in gid.keyValues().items(): gid_str+=3D'#%s#%s'%(pk, value) return gid_str =20=20=20=20=20=20 def str_to_gid(str): import string l=3Dstring.split(gid_str, '#') l.reverse() d=3D{} name=3Dl.pop() while l: d.setdefault(l.pop(), int(l.pop())) from Modeling.GlobalID import KeyGlobalID return KeyGlobalID(name, d) You then just have to request ec.faultForGlobalID() with the gid and you = get your object back. This does not work with temporary GlobalIDs, but if you're not going to use any sessioning mechanism as stated in your first message then you'll probably do not need to refer to inserted but unsaved objects! -- S=E9bastien. ------------------------------------------------------------------------ Index: EditingContext.py =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D RCS file: /cvsroot/modeling/ProjectModeling/Modeling/EditingContext.py,v retrieving revision 1.21 diff -c -r1.21 EditingContext.py *** EditingContext.py 12 Mar 2003 15:45:54 -0000 1.21 --- EditingContext.py 18 Mar 2003 00:29:39 -0000 *************** *** 1360,1365 **** --- 1360,1372 ---- self.recordObject(obj, key_gid) db=3Dself.rootObjectStore().objectStoreForObject(obj).database() db.incrementSnapshotCountForGlobalID(key_gid) + # time to update the object if a PK is set as class properties + from ModelSet import defaultModelSet + entity=3DdefaultModelSet().entityNamed(obj.entityName()) + pk_values=3Dkey_gid.keyValues() + for pk in entity.primaryKeyAttributes(): + if pk.isClassProperty(): + obj.takeStoredValueForKey(pk_values[pk.name()], pk.name()) else: obj=3Dself.objectForGlobalID(gid) new_gid=3Dnotification.userInfo().get(gid) ------------------------------------------------------------------------ |
From: Sebastien B. <sbi...@us...> - 2003-03-18 01:39:51
|
I wrote: > You then just have to request ec.faultForGlobalID() with the gid and > you get your object back. >=20 > This does not work with temporary GlobalIDs, but if you're not going > to use any sessioning mechanism as stated in your first message then > you'll probably do not need to refer to inserted but unsaved > objects! And this would maybe appear more straightforward than having to build a FetchSpec based on the id, wouldn't it?-) soif> Hy all, so this is for big take a look at http://larsen-b.com=20 soif> As you can see, i got modeling working well :)=20 That's nice. I roamed a bit around the cvsweb ; now you can get rid of Article.getID() and its ugly docstring ;) More seriously what I said in the previous msg. about using GlobalIDs would make it possible to serve objects from a single generic manager, rather than from a dedicated one for each ''type'' of id (such as in ArticlesManager.getArticleWithID()). My .02.=20 This should also appear in a FAQ I guess. Now time to sleep! Cheers, -- S=E9bastien. |
From: Sebastien B. <sbi...@us...> - 2003-03-27 12:48:44
|
Sebastien Bigaret <sbi...@us...> wrote: > [snip] > > >=20 > > > You are using getId() (returning article._id) instead of getID() > > > (returning the PK value stored in its GlobalID) ! > > >=20 > > >=20 > > > However, we have a bug here: Article's PK did not get its value. > >=20 > >=20 > soif> Yeah you got it :)=20 > soif> If don't use the id as classProperty as said in the doc, and use=20 > soif> the getID() helper sent before on the list it works well :)=20 >=20 > I included a small patch at the end of the message to solve this issue > --I'll make the unittest tomorrow and will correct this on the cvs > rep. Now article._id should get its value as expected after > ec.saveChanges(), if it's set as a class property. Done, you'll find it on cvs, EditingContext v1.22. Yannick, this may be of some interest to you as well: as far as I remember you declare your PK as class properties, too. -- S=E9bastien. |
From: Yannick G. <ygi...@yg...> - 2003-03-27 13:31:28
|
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Thursday 27 March 2003 07:48, Sebastien Bigaret wrote: > Yannick, this may be of some interest to you as well: as far as I > remember you declare your PK as class properties, too. Yes, I used to make a ugly hack :=20 snapshot =3D obj.snapshot() snapshot.update(self.__ec.globalIDForObject(obj).keyValues()) I debuged quite =E0 long time to find that saveChanges() did not set the value into the object... Thanks for the info ! : ) - --=20 Yannick Gingras Coder for OBB : Optically Bookable Barbados http://OpenBeatBox.org -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.7 (GNU/Linux) iD8DBQE+gv0srhy5Fqn/MRARArMnAKCAc3KNgNuX3pTRcB4RcULkmB1z7ACcC6uh /up0+hY1E+gvvCwvuSSuws8=3D =3DAvNq -----END PGP SIGNATURE----- |