Submit form items as list?

I have a submitform but it dos not look like I get the right format for each item when it is submitted.

  1. In the image below, the first 3 items are dummy data I made directly in the database. The two last items are submitted.
    It looks like they are stored as lists.

2.In my code I have to make the input into a string or else I get an error. I thought that inputs in python was a string from the get go.
image

  1. Code

Please post the errors here wrapped in backticks including the line that generates the error. Most folks here are volunteers so it really helps if you pose questions in a way that makes it as easy as possible for others to help you. - @alcampopiano

With that out of the way, we are probably going to need to see what is going into getMachineName and getMachineTypeSelected when the ‘add_newreadingName’ function is called (I am guessing from code in a form?).

Then we can see what is making your strings look like lists turned into strings.

Optional:
This last part is a python style nit-pick and not that important to make what you are doing work. Variables typed in camelCase are generally reserved for calling classes that usually create an object of that class. (Actually they use CapWords aka UpperCamelCase but :man_shrugging: )
The python style usually uses snake_case (all lower case with underscores instead of space) to denote variables and function parameters (like arguments)

Thank for your input @ianb
I will try to ask again with more context.

  1. I have a text input field, a dropdown and a datepicker in my form.
  2. When the submit button is clicked the all inputs are stored in the database.
    The text input and the dropdown I imagine should just save the inputs as strings.
    Later I will make a plot or table that counts similar input names and filtre by the dropdown elements
  3. In the image above from the Table0 it looks like the inputs are stored as list elements? is this a “problem”? Or “more difficult” to work with later for me?
    In the image of the code above I make the input into a string before it is saved in the table. Should this be done in any other way?

TLDR: It looks like the getMachineTypeSelected function parameter (the argument fed to the server module call) is populated with a list with one element in it, and you are calling str() on it

So you are passing it [‘a250’] which is literally a list with one string object in it as the only item in the list.

In the table insert you are calling str() on this list, so it is making the list human readable and then putting it in the text field, so it looks like how a human would type that list in string form.

When dealing with databases of any kind in python the column types you set almost always correspond to the basic data types python uses in some way, so a ‘Text’ column holds strings, a ‘Number’ holds integers or floats etc. Anvil has some special ones that hold python objects themselves for ease of use.

More info:
https://realpython.com/python-data-types/

The text column you are writing to should give you an error if you tried to give it a list instead of a string, but you circumvented the error by turning your list into a string that looks like a list.

Does that make sense?

you can either modify your code where you are calling this function to pass in getMachineTypeSelected as a string (I’m guessing by accessing the first element in the list like list[0], if you only have a single item)
Or you can iterate over the list inside this function if you know you will always be sending it a list and the first element of that list is the only thing you want to insert.

If you are trying to insert a list of items and not just the first one, you can iterate over the list like

for one_single_machine_type in getMachineTypeSelected:
  #code here that looks like your picture above but with
  # 'one_single_machine_type' in place of 'getMachineTypeSelected'

…you can drop the str() around the variables you insert.

The Python language has several philosophical reasons why things are done in a certain way, because of the way it is designed. It sounds silly but in this case, the error reporting is incredibly verbose and descriptive and will help you code.
One of the core ideas in python is to try something, and let it fail (“It is better to seek forgiveness than ask permission”)
If instead of ‘making sure what gets put into a text column is a string’ you ran the program, it would have given you this error:

anvil.tables.TableError: Column 'Type' is a string - cannot set it to a simpleObject
Learn more about Data tables 


  <running on the server>
  called from /libanvil/anvil/_server.py, line 41
  called from ServerModule1, line 25
  called from Form1, line 16

Which if you read carefully, is actually a condensed version of everything I just wrote.
(The real TLDR) :slightly_smiling_face: :cake: :eyes: