Something like this?

>>> class DALRef(int):
...     def __init__(self,table,value):
...             int.__init_(self,value)
...             self._table=table
...             self._record=None
...     def __getattr__(self,key):
...             if not self._record:
...                     self._record=self._table[int(self)]
...             return self._record[key]

and in sql.py

 row[tablename][fieldname]  = DALRef(self._db[referee],rid)

On Oct 27, 11:37 am, mdipierro <[email protected]> wrote:
> I have a better solution:
>
> >>> class A(int): pass
> >>> x=A(100)
> >>> print x
> 100
> >>> x+40
> 140
> >>> isinstance(x,int)
>
> True
>
> Yet x in an object and can have attributes. We can define a new class
> that works like and the reference id belongs to this class.  When you
> request an attribute of it, you get the attribute of the record. The
> SQL select would be done only on demand and cached by object so the
> values are there if needed again.
>
> This may be a bit tricky with the new API that allow reference by non-
> id primary key.
>
> Massimo
>
> On Oct 27, 11:29 am, Thadeus Burgess <[email protected]> wrote:
>
> > I would like this.
>
> > select_refs would get passed to the select() function, this way I did not
> > have to write a join.
>
> > Perhaps it could be called join_foreign, defaulted to False
>
> > Say a student is assigned to a teacher.
>
> > db(db.students.student_id==1102926).select(foreignkey_join=True)
>
> > yeilds
>
> > student.id student.name student.email student.phone
> > student.teacher.namestudent.teacher.room_number
>
> > and
>
> > db(db.students.student_id==1102926).select()
>
> > yeilds where student.teacher is the id.
>
> > student.id student.name student.email student.phone student.teacher
>
> > This also allows us to expand to provide auto many-to-many joins as well.
> > This would help add more of an ORM aspect to our DAL without losing any of
> > the sweetness of the DAL.
>
> > -Thadeus
>
> > On Tue, Oct 27, 2009 at 11:21 AM, mr.freeze <[email protected]> wrote:
>
> > > Too bad, other DAL/ORMs have this. SQLAlchemy calls it a backref and
> > > django calls it a reverse foreign key manager.  What if Field had a
> > > 'select_refs=False' attribute so it was configurable?
>
> > > On Oct 27, 7:32 am, mdipierro <[email protected]> wrote:
> > > > This is not backward compatible, it also make a hidden select every
> > > > time you access a record. So if you select 100 records that have
> > > > reference and loop over them, you'd be making 100 additional selects.
> > > > If this is what you want to do, I think it is better to be explicit or
> > > > do a join.
>
> > > > On Oct 27, 1:43 am, "mr.freeze" <[email protected]> wrote:
>
> > > > > I made a simple patch that lets this work and is seemingly backwards
> > > > > compatible (didn't break my apps anyway).  It basically automatically
> > > > > does a select for any reference fields so you can do this (db.things
> > > > > has a reference field 'author' to auth_user in this example):
>
> > > > > record = db(db.things.id==1).select()[0]
> > > > > print record.author.email
>
> > > > > What do you think?
>
> > > > > ### Eclipse Workspace Patch 1.0
> > > > > #P web2py
> > > > > Index: gluon/sql.py
> > > > > ===================================================================
> > > > > --- gluon/sql.py        (revision 1310)
> > > > > +++ gluon/sql.py        (working copy)
> > > > > @@ -451,6 +451,8 @@
> > > > >              else:
> > > > >                  return "'F'"
> > > > >      if fieldtype[0] == 'i':
> > > > > +        if isinstance(obj,DALStorage):
> > > > > +            return str(int(obj.id))
> > > > >          return str(int(obj))
> > > > >      elif fieldtype[0] == 'r':
> > > > >          if fieldtype.find('.')>0:
> > > > > @@ -2926,7 +2928,7 @@
> > > > >              if field.type[:10] == 'reference ':
> > > > >                  referee = field.type[10:].strip()
> > > > >                  rid = value
> > > > > -                row[tablename][fieldname] = rid
> > > > > +                row[tablename][fieldname] = 
> > > > > self._db(self._db[referee]
> > > > > ['id']==rid).select()[0]
> > > > >              elif field.type == 'blob' and value != None:
> > > > >                  row[tablename][fieldname] = base64.b64decode(str
> > > > > (value))
> > > > >              elif field.type == 'boolean' and value != None:
>
> > > > > On Oct 26, 11:47 pm, "mr.freeze" <[email protected]> wrote:
>
> > > > > > Also, why doesn't this work?:
>
> > > > > > record = db(db.tablename.id==1).select()[0]
> > > > > > print record.author.email
>
> > > > > > It seems like it should work since author is a reference to 
> > > > > > auth_user
> > > > > > and it's referencing a distinct record.
>
> > > > > > On Oct 26, 11:25 pm, mdipierro <[email protected]> wrote:
>
> > > > > > > Given:
>
> > > > > > > ... Field("author", db.auth_user, default=auth.user.if if 
> > > > > > > auth.user
> > > > > > > else 0) ...
>
> > > > > > > db.tablename.author.requires=IS_IN_DB(db,'auth_user.id
> > > ','%(first_name)
> > > > > > > s %(last_name)s')
>
> > > > > > > You can do
>
> > > > > > > record = db(db.tablename.id>0).select().first()
> > > > > > > print db.auth_user[record.author].first_name
>
> > > > > > > and/or
>
> > > > > > > row= db(db.tablename.author==db.auth_user.id).select.first()
> > > > > > > print row.tablename, row.auth_user.first_name
>
> > > > > > > On Oct 26, 10:58 pm, Wiiboy <[email protected]> wrote:
>
> > > > > > > > And then how would I get the user's info?
>
> > > > > > > > db(db.auth_user.id == db.tablename.author)
>
> > > > > > > > ?
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to