If I understand what you need, your problem is representing a record
(not a specific field) in SELECT/OPTION when creating/updating a
record in a different table that references this one.

If this the case, the list of options is built by the IS_IN_DB
validator.

It looks at the format string, figures out which columns are needed
and fetches all records needed.

One solution could be to allow format to be a function. If format were
to be a function, instead of a string, it would fetch all columns and
just call the function once for each of them. It would be slower but
not much worse. It could be implemented easily.





On Jan 8, 8:31 am, "KONTRA, Gergely" <pihent...@gmail.com> wrote:
> First, I am not debugging the code, just browsing it, and found that:
>
> def sqlhtml_validators(field):
>     """
>     Field type validation, using web2py's validators mechanism.
>
>     makes sure the content of a field is in line with the declared
>     fieldtype
>     """
>     field_type, field_length = field.type, field.length
>     if isinstance(field_type, SQLCustomType):
>         if hasattr(field_type,'validator'):
>             return field_type.validator
>         else:
>             field_type = field_type.type
>     requires=[]
>     if field_type == 'string':
> # ....
>     elif field._db and field_type[:9] == 'reference' and \
>             field_type.find('.')<0 and \
>             field_type[10:] in field._db.tables:
>         referenced = field._db[field_type[10:]]
>         if hasattr(referenced,'_format') and referenced._format:
>             def f(r,id):
>                 row=r[id]
>                 if not row:
>                     return id
>                 else:
>                     return r._format % row
>             field.represent = lambda id, r=referenced, f=f: f(r,id)
>             requires = validators.IS_IN_DB(field._db,referenced.id,
>                                            referenced._format)
>             return requires
>
> I am also not aware of web2py internals, so I have no idea how the
> code above could figure out which columns to load for using the
> format, so absolutely no idea. Also could not find anything that shwos
> me any optimization which fields are loaded when using format in
> define_table...
>
> I hope there is an object cache, so I will not have to fetch objects
> one-by-one. Querying them once again should not harm...
>
> On Fri, Jan 8, 2010 at 14:01, mdipierro <mdipie...@cs.depaul.edu> wrote:
> > What do you want to do with it? Are you planning to fecth one record
> > at the time for every record in all possible references? I would not
> > recommend it.
>
> > I am not saying no to your proposal of a represent function. I am just
> > saying we need to think it over more.
>
> > In the end what you want (and makes sense) is a simpler syntax for
> > merging format with virtualfields. I think if we do this we must do it
> > well and it is not a minor change.
>
> > Massimo
>
> > On Jan 8, 6:42 am, "KONTRA, Gergely" <pihent...@gmail.com> wrote:
> >> Well, then let the function accepts not a row, but an id, which is the
> >> pk of the row.
>
> >> On Fri, Jan 8, 2010 at 13:01, mdipierro <mdipie...@cs.depaul.edu> wrote:
> >> > Here is the problem. Perhaps you have a solution.
>
> >> > format is used to represent a record in a reference as a select/
> >> > option. Probably you have many of these records and you only want to
> >> > fetch columns that are necessary to build the representation.
>
> >> > By using format='%(first_name)s %(last_name)s' web can parse it and
> >> > easily figure out that you only need first_name and last_name.
>
> >> > My example above is convoluted but it allows you to create an
> >> > arbitrary representation and specify exactly which fields you need.
>
> >> > By using represent you are passing a (lambda) function. How would
> >> > web2py determine which columns to fetch? Should it fetch them all? It
> >> > could be slow.
>
> >> > Massimo
>
> >> > On Jan 8, 5:13 am, "KONTRA, Gergely" <pihent...@gmail.com> wrote:
> >> >> Since the patch is about 2 lines of code, does it make sense to have a
> >> >> represent for tables, similar for fields, so that I could write
> >> >> something like:
>
> >> >> db.post.represent = lambda row: row['title'], which means the same as
> >> >> using format='%(title)s' on define_table?
> >> >> thx
> >> >> Gergo
>
> >> >> On Thu, Jan 7, 2010 at 20:32, mdipierro <mdipie...@cs.depaul.edu> wrote:
> >> >> > Now I understand.
>
> >> >> > Short answer. It cannot be done in the sense there is no API for it
>
> >> >> > Long answer. It can be done this way:
>
> >> >> > ### define table post and set the format
> >> >> > ### based on a field that does not exist
> >> >> > db.define_table('post',
> >> >> >    Field('title',length=256),
> >> >> >    Field('body','text',requires=IS_NOT_EMPTY()),
> >> >> >    Field('author',db.auth_user),
> >> >> >    format='%(title_short)s',
> >> >> > )
>
> >> >> > ### define the field as a virtual field computed
> >> >> > class ComputedFieldsForPost:
> >> >> >    def title_short(self):
> >> >> >        return self.post.title[:40]+'...'
> >> >> > db.post.virtualfields.append(ComputedFieldsForPost())
>
> >> >> > ### define the referencing table
> >> >> > db.define_table('comment',
> >> >> >    Field('post',db.post,writable=False,readable=False),
> >> >> >    Field('author',db.auth_user,writable=False,readable=False),
> >> >> >    Field('body','text',requires=IS_NOT_EMPTY()))
>
> >> >> > ### modify an internal of the validator to
> >> >> > ### fetch data required to compute the virtual field
> >> >> > db.comment.post.requires.fields=['post.id','post.title']
>
> >> >> > This kind of ugly but powerful.
>
> >> >> > On Jan 7, 12:25 pm, "KONTRA, Gergely" <pihent...@gmail.com> wrote:
> >> >> >> Sure.
>
> >> >> >> db.py:
>
> >> >> >> db.define_table('post',
> >> >> >>    Field('title',length=256),
> >> >> >>    Field('body','text',requires=IS_NOT_EMPTY()),
> >> >> >>    Field('author',db.auth_user),
> >> >> >>    )
>
> >> >> >> db.define_table('comment',
> >> >> >>    Field('post',db.post,writable=False,readable=False),
> >> >> >>    Field('author',db.auth_user,writable=False,readable=False),
> >> >> >>    Field('body','text',requires=IS_NOT_EMPTY()))
>
> >> >> >> In the example I provided, in the appadmin of comment, auth_user is
> >> >> >> displayed as a dropdown. I would like to do that for comment.post
> >> >> >> also.
>
> >> >> >> The format option make it possible to hack up some fields of the
> >> >> >> database, but it is not so flexible to display for eg. computed 
> >> >> >> values
> >> >> >> of the post table.
>
> >> >> >> thanks in advance
> >> >> >> Gergo
> >> >> >> On Thu, Jan 7, 2010 at 16:35, mdipierro <mdipie...@cs.depaul.edu> 
> >> >> >> wrote:
> >> >> >> > Hi Gergo,
>
> >> >> >> > appadmin has nothing to do with t2 and t2 was deprecated long ago.
>
> >> >> >> > I guess you can still define db.post.represent but I am not sure 
> >> >> >> > what
> >> >> >> > do you want to do with it. What behavior do you expect? Can you
> >> >> >> > provide a concrete example?
>
> >> >> >> > On Jan 7, 9:31 am, "KONTRA, Gergely" <pihent...@gmail.com> wrote:
> >> >> >> >> Yes, AFAIK appadmin is a T2 thing.
>
> >> >> >> >> So, I wonder how can one rewrite the line below using
> >> >> >> >> db.post.represent (still that is more flexible)
>
> >> >> >> >> db.define_table('post',
> >> >> >> >>     Field('title',length=256),
> >> >> >> >>     Field('body','text',requires=IS_NOT_EMPTY()),
> >> >> >> >>     Field('author',db.auth_user),
> >> >> >> >>     format='%(title)s')
>
> >> >> >> >> db.post.represent = ?
>
> >> >> >> >> thanks
> >> >> >> >> Gergo
>
> >> > --
> >> > You received this message because you are subscribed to the Google 
> >> > Groups "web2py-users" group.
> >> > To post to this group, send email to web...@googlegroups.com.
> >> > To unsubscribe from this group, send email to 
> >> > web2py+unsubscr...@googlegroups.com.
> >> > For more options, visit this group 
> >> > athttp://groups.google.com/group/web2py?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups 
> > "web2py-users" group.
> > To post to this group, send email to web...@googlegroups.com.
> > To unsubscribe from this group, send email to 
> > web2py+unsubscr...@googlegroups.com.
> > For more options, visit this group 
> > athttp://groups.google.com/group/web2py?hl=en.
>
>
-- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To post to this group, send email to web...@googlegroups.com.
To unsubscribe from this group, send email to 
web2py+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/web2py?hl=en.


Reply via email to