Hello annet and Massimo!

Thank you very much for your help! I still need some enlightenment
though =)

I've managed to make it work thanks to annet's tip. Here's how I am
currently doing it:

I have defined a helper function that takes a teacher_id and spits out
the dropdown field (!) generated through annet's method:

def subject_biased_dropdown(teacher_id):
    rows = db().select(db.subjects.id, db.subjects.code,
        left = db.teachers_subjects.on(
            (db.subjects.id==db.teachers_subjects.subject_id)&
            (db.teachers_subjects.teacher_id==teacher_id)),
        orderby=db.teachers_subjects.teacher_id|db.subjects.id)
    key = [row.id for row in rows]
    value = [row.name for row in rows]
    form = SQLFORM.factory(
        Field('subject_id', requires=IS_IN_SET(key,value,zero=None)))
    return form[0][0]

And then, in the "evaluation/create" function of the "evaluation"
controller, I replace the dropdown field in the autogenerated SQLFORM
with the "biased" one that comes from the above function:

def create():
(...)
    form_add=SQLFORM(db.evaluations,
    (...)
    )
    form_add[0][0] = subject_biased_dropdown(prof_id)
    if form_add.accepts(request.vars, session):
    (...)
    return dict(form=form_add)

Yes, it works, but I feel something strange about the way I'm
implementing this. I guess I'm missing something obvious that would
make it easier/more "best-practice-like". Defining the orderby at the
model level seems like a better option, but I can't figure it out yet.

Again, thank you very much for your help!

Regards,

Tiago Rosa

On Oct 6, 2:01 pm, annet <[email protected]> wrote:
> Massimo,
>
> > you can pass orderby to IS_IN_DB
>
> To which IS_IN_DB do I have to pass the orderby to get the same
> result. The resultset reads like:
>
> 1     1   a
> 1     4   d
> 1     7   9
> None  2   b
> None  3   c
> None  5   e
> None  6   f
>
> The orderby: orderby=~db.teachers_subjects.teacher_id|db.subjects.id
> first sorts it in reverse order by db.teachers_subjects.teacher_id and
> then by db.subjects.id, to meet Tiago's requirement:
>
> When creating a new evaluation for teacher t1, the items on the
> subject dropdown should appear in the following order:
>
> a      //related
> d      //related
> g      //related
> b
> c
> e
> f
>
> Annet.

Reply via email to