Deepak
Deepak

Reputation: 1545

how to put the field name with a drop-down instead of its reference id in the form?

this is model x.py:

# coding: utf8
db.define_table('dept',
            Field('name',unique=True,label='Department Name'))

db.define_table('course',
            Field('dept_id','reference dept'),
            Field('name',unique=True,label='Course Name'))

this is is controller x.py:

# create a new department:
def create_dept():
    form = SQLFORM(db.dept).process(next=URL('show_dept'))
    return dict(form=form)

# list all departments
def show_dept():
    rows = db().select(db.dept.ALL)
    return dict(rows=rows)

# create new course:
def create_course():
    form = SQLFORM(db.course).process(next=URL('show_all_course'))
    return dict(form=form)

# list all courses
def show_all_course():
    rows = db().select(db.course.ALL)
    return dict(rows=rows)

now this is view of x/create_course:

{{extend 'layout.html'}}
<h1>This is the x/create_course.html template</h1>
<h2>
    Enter the name of the course you want to create
</h2>
{{=form}}

now my question is -> when I create a new course I have to fill in the the dept(department) id myself how can I make it a dropdown where the the department names are shown and I select a department name and enter the new course name and press submit and it creates a new course in that department.

Upvotes: 1

Views: 89

Answers (1)

Anthony
Anthony

Reputation: 25536

db.define_table('dept',
    Field('name',unique=True,label='Department Name'),
    format='%(name)s')

If you give the db.dept table a "format" attribute (as above), then any fields that reference it will get a default IS_IN_DB validator, which will produce a drop-down with values based on the "format" attribute (the reference field will also get a default "represent" attribute based on the "format" attribute).

Upvotes: 2

Related Questions