Here are my GAE DALnr snippets.
Some code has been simplified (by using if stmts with hard-coded
classnames) for brevity.
I will provide a link to a zip of a working sample next Monday.
In gluon\contrib\gql.py:
---------------------------------------------------------------------------------------------------
from google.appengine.ext import db as google_db
from google.appengine.ext.db import polymodel as pm # DLypka Mod to
support PolyModel
:
:
class MyGAEBaseDALnr(pm.PolyModel): #DLypka Mod
selflinkPrev = google_db.SelfReferenceProperty
(collection_name='selflinksPrev')
selflinkNext = google_db.SelfReferenceProperty
(collection_name='selflinksNext')
class MyGAEBaseDALnrSegment(pm.PolyModel): #DLypka Mod This entity
is a child of MyGAEBaseDALnr
linkToDALnr = google_db.ReferenceProperty(MyGAEBaseDALnr,
required=False, collection_name='linksToDALnr')
# DLypka Mod: New method
def insertNative(self, **fields):
self._db['_lastsql'] = 'insert'
for field in self.fields:
if not field in fields and self[field].default != None:
fields[field] = self[field].default
if field in fields:
fields[field] = obj_represent(fields[field], self
[field].type, self._db)
tmp = self._tableobj(**fields)
tmp.put()
return dict(nativeRef=tmp, id=tmp.key().id())
In Method def _select(self, *fields, **attributes):
:
:
# tablename = table.kind() # DLypka commented out
isPoly = isinstance(table, pm.PolymorphicClass) # DLypka
Patch for PolyModel support
if isPoly: # DLypka Patch for PolyModel support
tablename = table.class_name() # DLypka Patch for
PolyModel support
else: # DLypka Patch for PolyModel support
tablename = table.kind()
In Method def _create(self):
:
:
#Note: The specific class names are hardcoded in the following if()
stmts for brevity in this snippet.
# This hard coding is replaced in my complete version of my
DALnr
# with a new named parameter 'gaebaseclass'
# added into the define_table() method in gql.py
if self._tablename == 'MyGAEBaseDALnr':
self._tableobj = classobj(self._tablename,
(MyGAEBaseDALnr, ), myfields)
elif self._tablename == 'MyGAEBaseDALnrSegment':
self._tableobj = classobj(self._tablename,
(MyGAEBaseDALnrSegment, ), myfields)
else:
self._tableobj = classobj(self._tablename,
(google_db.Model, ), myfields)
return None
==========================================================================================================
In your applications\init\models\db.py:
----------------------------------------------------------------------------------------------------------
db.define_table('DALnr',
db.Field('name','text')
)
db.define_table('DALnrSegment',
db.Field('idDALnr',db.DALnr), # web2py reference to
parent
db.Field('level','integer'),
db.Field('name','text')
)
myDALnrNativeInstance00keys = db.DALnr.insertNative
(level=0,name='DALnr_01') # I cloned insert() but I return a dict()
with the native ref as well as id
myDALnrNativeInstance00 = myDALnrNativeInstance00keys['nativeRef']
myDALnrNativeInstance00id = myDALnrNativeInstance00keys['id']
rows=db(db.DALnr.id > 0).select()
for row in rows:
myDALnrInstance00 = row # NOTE: There is only a single row here,
so myDALnrInstance00 will be the row we just inserted into the web2py
DAL
myDALnrSegmentNativeInstance00_00keys = db.DALnrSegment.insertNative
(idDALnr = myDALnrNativeInstance00id, level=0,
name='DALnrSegment_00_01')
myDALnrSegmentNativeInstance00_00 =
myDALnrSegmentNativeInstance00_00keys['nativeRef']
myDALnrSegmentNativeInstance00_00.linkToDALnr =
myDALnrNativeInstance00 # Link this child to its parent
myDALnrSegmentNativeInstance00_00.put() # This rewrites (updates) the
entity to have the updated value in linkToDALnr set in the previous
stmt
# <=== NOTE This is the Interesting part !!! =======
links1 = myDALnrNativeInstance00.linksToDALnr # <=== NOTE: this
property was AUTOMATICALLY maintained by the automatic bidirectional
nature of the ReferenceProperty, so that the 'parent' has a list of
refs to its related children, without any explicit code to add
them!!!
# links1 is a google.appengine.ext.db.query
# It is a db.Query
# loop on links1 which is a column on the parent entity and see what
you get:
for key in links1:
childref = key # childref is (magically) a MyGAEBaseDALnrSegment
which is a child of MyGAEBaseDALnr
-- End of DALnr Snippets for GAE ---
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---