Drop down from Table

Hello, here is what I am trying to do, following Anvil Docs | Dropdowns and Data Tables
of course, it doesn’t work.
I hope you can simplify this…but until then I need to have it working.
I have a table called “horses” and another called “expenses”. I need to create a new expense associated to a given horse.
So my first step is to populate a drop down with all horses names. Later in the future will add users so will need to filter, but that will be another problem :slight_smile:

Following the example I did:
self.drop_down_horses.items = [(r['Name'], r) for r in app_tables.horses.search()]

and I get:

Exception: Dropdown item tuples must be of the form (‘label’, value)

until this is fixed, anyone here can help me with this?
Thanks!

ok fixed…the error was happening because the Name for one row on table Horses was empty…
:speak_no_evil:

Well not really solved…now I need to validate the form to avoid blanks…no easy way to do it
:zipper_mouth_face:

Presumably you don’t want to display a horse in the drop down if it has no name?

[(r['Name'], r) for r in app_tables.horses.search() if r['Name'] is not None]

or similarly

[(r['Name'], r) for r in app_tables.horses.search(Name=q.not_(None))]
1 Like

Hi, thanks for the answer.
Well not sure about the empty name, an empty name will lead the user to spot the error, maybe?
Anyways friendly error message would be better.
Also enforcing on the horse creation form that users include a name should be way more easy. I, honestly, don’t want to spend hour researching, learning, doing and testing things that are standard on every web page and you will need on 99% of forms you create.

you could always do

[(str(r['Name']), r) for r in app_tables.horses.search()]

or yes ensure that all horses have names :horse:

But you’re right - it would be good if anvil had your back on this one and just converted the first element of the tuple into a string…

A bit like when you bind a text property of a label to a None value.