Trouble retrieving specific data from anvil database

What I’m trying to do:
It is my first time coding in a long time and I’m am very new to Anvil so please forgive me if I am making very simple mistakes that I have overlooked.
I am creating a time management app for school and I have saved user time schedules into a database along with a UUID. I am trying to search for the UUID in the database by showing the user all their saved schedules and then using that UUID display those in a separate form by saving the uuid in a separate form.
What I’ve tried and what’s not working:
I’ve managed to display the saved schedules from the user but whenever I am getting the uuid from the selected schedule that the user wants to display it always returns as a live object search iterator. And when trying different methods I’ve found other errors such as: ‘anvil.tables.TableError: Column ‘uuid’ can only be searched with a string (not a simpleObject) in table ‘Schedules’’. Either way my end goal of displaying this schedule has not worked out yet
Code Sample:

#server module
@anvil.server.callable
def get_schedule_id(subject, as_number):
  if anvil.users.get_user() is not None:
    result = [{subject: r["Subject"], as_number: r["as_number"]} for r in app_tables.schedules.search()]
    return result
#calling form
  def schedule_select_click(self, **event_args):
    """This method is called when the link is clicked"""
    Globals.selected_schedule = self.schedule_title.text
    Globals.schedule_as_number = self.schedule_as.text
    uuid = anvil.server.call('get_schedule_id', Globals.selected_schedule, Globals.schedule_as_number)
    uuid = Globals.selected_schedule_id
    alert(content=schedule_show(), large=True)

There is also the displaying of the schedule where I am using a repeating panel and binding each of the labels in the repeating panel to a certain part of the schedule that I should be retrieving through the uuid:

class schedule_show(schedule_showTemplate):
  def __init__(self, **properties):
    # Set Form properties and Data Bindings.
    self.init_components(**properties)
    self.refresh()
    # Any code you write here will run before the form opens.
  def refresh(self):
   # schedule = anvil.server.call('schedule_by_id', Globals.selected_schedule_id)
    self.schedule_panel.items = anvil.server.call('schedule_by_id', Globals.selected_schedule_id)

Please let me know how useful it would be for me to share a copy of my app because I would need to omit quite a lot of stuff first!

Thanks in advance

Hi @oli.medwin !

First of all, your get_schedule_id isn’t doing nothing with the received parameters. In fact, it’s not clear to me what do you want to do in that function.

If you want to retrieve all schedules from that user, do something like that:

@anvil.server.callable
def get_user_schedules():
  if anvil.users.get_user() is not None:
    rows = app_tables.schedules.search(user=anvil.users.get_user())
    return list(rows)

And here you overwrites uuid variable after setting it with the server call:

    uuid = anvil.server.call('get_schedule_id', Globals.selected_schedule, Globals.schedule_as_number)
    uuid = Globals.selected_schedule_id

This is not right…

And why do you need and UUID? Anvil already has an unique id in the format “[999,8888]”.

I suggest you take a look at Tutorials that do you what you’re willing to do:

Welcome to the Forum!

No worries! You’re in plenty of company.

Every programming language has its “quirks”, including Python, and Anvil adds its own on top of that. In this case, you’ve stumbled on the topic of data types.

  1. Data type determines what you can do (directly) with a value.
  2. Unlike C/C++, in Python, a value’s type is part of the value, not the variable, which simply refers to its “current value”. Thus, any variable can receive any type of value – including types you weren’t expecting, such as an Anvil-supplied database table search iterator.
  3. Anvil provides many different types of values. Often, these are intended to work a lot like some of Python’s built-in types, so you can use the same idioms (coding patterns) that are “natural” to Python. But if you’ve been away from coding for awhile, these patterns may not be obvious, especially if you’re not coming from a Python background.

I came from a C/C++ background, so I had quite a few patterns (idioms) to learn. In particular, I had to figure out how to turn the types I was getting into the types I wanted. The details will depend on the types you’re getting. Since these types come from Anvil, that means getting into the relevant parts of the Anvil documentation. (E.g., Using Data Tables from Python.)

For example, if you’re getting an iterator, when you wanted a database table row, then you’re probably calling search instead of get. (Getting One Row from a Table)

Pulling all the pieces together doesn’t happen overnight. But it does come together.

6 Likes