0

With two tables:

db.define_table('content',
    # Here I want to store the id of the chosen layout_type
    Field('layout_type_id', db.layout_type),
    # Here the plain name - might not be necessary
    Field('layout_type_name', 'list:string')
    )

db.define_table('layout_type',
    Field('layout_type_name', 'string')
    )

db.content.layout_type_name.requires = IS_IN_DB(db, 'layout_type.id', '%(layout_type_name)s')

I create a form

form = crud.update(db.mytable, id)

where I want a Dropdown menu to choose a layout_type_name from - which is working - and store the id of the chosen entry.

How is it possible to store the id of the chosen layout_type_name in content.layout_type_id?

Also, if I choose one entry in the dropdown mentioned above - the (text)value is not stored when the form is successfully submitted.

Rockbot
  • 935
  • 1
  • 6
  • 24
  • This is not how reference fields work. A reference field stores the record ID of the referenced record -- it does not store values of other fields from the referenced record -- that defeats the purpose of the foreign key, as you are denormalizing the data. Please explain what you are trying to achieve. In particular, do you need to store a list of names or IDs in the table_type_name field, or just a single name/ID? – Anthony Apr 20 '16 at 16:03
  • In general I want to store different layout types. To know which one to choose I use the dropdown list which is generated from the available entries. What I want to store is the id of the chosen entry. I will edit the question and try to make this more clear – Rockbot Apr 20 '16 at 16:31

1 Answers1

1

These changes made this problem work:

db.define_table('content',
    Field('content_type_id', db.content_type)
    )

db.define_table('layout_type',
    Field('layout_type_name', 'string'),
    format='%(layout_type_name)s'
    )

This post about record representation is exactly what I was initially looking for.

Community
  • 1
  • 1
Rockbot
  • 935
  • 1
  • 6
  • 24
  • 1
    Your code is confusing -- the `content_type_id` field references the `db.cms_content_type` table, yet its `IS_IN_DB` validator checks records in a different table (i.e., `db.layout_type`). Either there is an error in your code, or the code will not work as you expect. – Anthony Apr 21 '16 at 14:20
  • 1
    Also, there is no need to specify the `IS_IN_DB` validator explicitly. Instead, you can set the "format" argument of the `db.layout_type` table to '%(layout_type_name)s`, and the validator will be created automatically. – Anthony Apr 21 '16 at 14:21
  • @Anthony Thanks for the sharp eye - I completely messed the code up... I found an older post from you about the same problem. – Rockbot Apr 21 '16 at 16:15