Initial dropdown selections not being passed to dictionary

I’m having issues with my dropdown selections not being passed to a dictionary if I don’t change the initial selection:
Here you can see my form where I have chosen the initial selection on my dropdown

Here you can see the error it creates

However, when I change the selections on the dropdowns…

…It passes the selection to the dictionary

It is a relatively new bug which I haven’t encountered before. This form was working previously.

Could you provide some code samples and/or a clone link? While the pictures tell a story code tells a better one.

Thanks for your reply. Is it possible to provide the clone link privately?

In terms of code for the form and save button:

  def button_new_control_click(self, **event_args):
    # Initialise an empty dictionary to store the user inputs
    new_control = {}
    # Open an alert displaying the 'Control_Form' Form
    save_clicked = alert(content=File_Control_Form(item=new_control),
      title="Add Control",
      large=True,
      buttons=[("Save", True), ("Cancel", False)],
         )
    
    # If the alert returned 'True', the save button was clicked.
    if save_clicked:
      anvil.server.call('add_quick_file_control', new_control)
      self.__init__()

This links to back end server code:

@anvil.server.callable
def add_quick_file_control(control_dict):
  item = app_tables.items.add_row(
    **control_dict
  )
  item['ID'] = item.get_id()
  item['Item_Owner'] = control_dict['Item_Owner']
  item['Item_Owner_Text'] = item['Item_Owner']['email']
  item['Frequency_Row'] = control_dict['Frequency_Row']
  item['Frequency'] = item['Frequency_Row']['Frequency']
  item['control_update_type_row'] = control_dict['control_update_type_row']
  item['control_update_type'] = item['control_update_type_row']['Types']

Let me know if that’s enough? If not, happy to share the clone link privately.

Generally it’s a good idea to provide a clone link that only demonstrate the behaviour that is causing the issue.

It’ll help others to debug the code.

So if it’s possible to decouple the issue into a smaller app that’ll help a lot - and will also mean you don’t have to worry about sharing sensitive data.

From the snippets it looks like you’re providing an empty dictionary as the item.

Writeback databindings won’t update unless the user interacts with the component so I’m not surprised that you’re getting a key error here if the user hasn’t changed the dropdown, and the item was initially an empty dictionary.

You could pre-populate the self.item in the __init__ method - or do a validation before the server call.

# something like this in the init method 
self.item.update({'Owner': 'someowner'}) # provide some default values
self.refresh_data_bindings()
2 Likes

Hi @stucork, thanks for your reply. I added a placeholder in the dropdown and a validation to the UI so users would have to select a dropdown item. Thanks again!

1 Like