Thank you Anthony !

Is the length "len" always defined in Python ?

I couldn't find much tools in the documentation to query lists of
references apart from the "contains" operator.

For example, let's have the following "message" table :
id / to          / status
1  / steve,jimmy / 0,2
2  / john,julia  / 1,2
3  / julia,peggy / 0,1

I want to get the rows where "Julia" is in "to" and where her status
is "0" (in this particular case, that is row n°3).
With the "contains" operator I only know how to get the rows where
"Julia" is in "to" (that is row n°2 and n°3).

Should I write raw SQL ?

Thanks,
Archibald


On 26 oct, 15:03, Anthony <abasta...@gmail.com> wrote:
> This is probably the easiest way to go. You could use a computed field, but
> then if you want the computed field to appear in an update form, you have
> to explicitly list it (along with all fields you want to appear). Note, if
> you instead used request.vars.to to set the field default, that wouldn't
> present any danger in terms of the contents of request.vars.to not being
> validated, because you're only using the length of request.vars.to. It
> would only be an issue if the validation of request.vars.to could result in
> it passing validation but its length changing as a result of that.
>
> Anthony
>
>
>
>
>
>
>
> On Wednesday, October 26, 2011 6:39:29 AM UTC-4, Archibald Linx wrote:
>
> > Thank you Anthony.
>
> > I will need to exclusively modify the variable "status" on other
> > occasions. So I am not sure I really want "computed fields". Is that
> > true ?
>
> > So I decided to try an onvalidation function and I ended up with :
>
> > def status(form):
> >     form.vars.status = [0]*len(form.vars.to)
>
> > def write():
> >     form = SQLFORM(db.message)
> >         if form.accepts(request.vars, session, onvalidation=status):
> >             response.flash = 'Got it'
>
> > It seems to work.
>
> > Is it ok ? If I have understood correctly, onvalidation is called
> > after validation and before insert. Like this I am sure
> > "forms.vars.to" does not contain anything fishy or dangerous.
>
> > Thanks,
> > Archibald
>
> > On 25 oct, 18:30, Anthony <abas...@gmail.com> wrote:
> > > On Tuesday, October 25, 2011 11:45:41 AM UTC-4, Archibald Linx wrote:
>
> > > > Thank you Anthony.
>
> > > > All this works very nicely.
> > > > Maybe the default format for auth_user should also be documented.
>
> > > > Let's say I have the following table :
> > > > db.define_table('message',
> > > >     Field('to', 'list:reference auth_user'),
> > > >     Field('status', 'list:integer'))
>
> > > > I want "status" to be a list of 0 as long as "to", but
> > > > "db.message.status.default = [0]*len(db.message.to.default)" does not
> > > > work.
>
> > > db.message.to.default represents the default value for the 'to' field
> > (which
> > > you have not set), not the actually values being inserted. Anyway, I
> > don't
> > > think the default for one field can reference the values of another.
>
> > > There are a few other options. You could use a computed field (though by
> > > default that won't display in a form, if you need that). If
> > inserts/updates
> > > will always be handled via form submissions, you could us a form
> > > onvalidation function or a custom validator. You could also just do
> > > default=[0]*len(request.vars.to), since upon form submission, the
> > values
> > > submitted to the 'to' field will be stored in request.vars.to (note,
> > the
> > > values stored in request.vars will be the unvalidated values). See here
> > for
> > > more
> > > details:
> >http://stackoverflow.com/questions/7325776/using-a-lambda-for-a-model...
>
> > > Anthony

Reply via email to