Api app_tables to filter and sort at the same time

Hello Anvil community.
I’m trying to filter a data grid between two dates. The filter works fine if I don’t sort the Fecha and Numero_Partida columns. I need to filter the Datagrid and sort the data at the same time.

What I’ve tried and what’s not working:
SyntaxError: Positional argument cannot follow keyword argument

Code
def btnBuscarFechas_click(self, **event_args):
“”“Filtra partidas contables entre dos fechas (inclusive la fecha final)”“”

fecha_inicio = self.fechaPrimera.date
fecha_fin = self.fechaSegunda.date

if not fecha_inicio or not fecha_fin:
  Notification("Debe seleccionar ambas fechas.", style="warning", timeout=2).show()
  return

if fecha_inicio > fecha_fin:
  Notification("La fecha inicial no puede ser mayor que la fecha final.", style="warning", timeout=2).show()
  return

# ✅ Sumar 1 día a la fecha final para incluir todos los registros de ese día
fecha_fin_incluyente = fecha_fin + timedelta(days=1)

partidas = app_tables.diario_partidas.search(
  Fecha=q.between(fecha_inicio, fecha_fin_incluyente),
  tables.order_by("Fecha", ascending=False),   #**HERE ERROR**
  tables.order_by("Numero_Partida", ascending=True) #**HERE ERROR**
)

self.repeating_Partidas.items = partidas

Have you tried putting the positional arguments (the order_bys) before the keyword argument (Fecha)?

3 Likes

In Python, positional arguments must always come before named arguments. This is because the order of positional arguments matters, while named arguments (those specified with a name) can be listed in any order after all positional arguments.

A positional argument is simply passed to a function by its position, without specifying the parameter name. Named arguments are those where you include the name, making them clear regardless of order.

In your code, the issue is that you have a named argument (Fecha) first, and then you’re adding two positional arguments. This causes a syntax error in Python.

To fix this, provide the two positional arguments first (watch the order, since it will affect your results), and only after that, pass any named arguments you need.

In summary, any named argument (like the one highlighted in red) must come after all positional arguments (like those highlighted in blue).

image

6 Likes

I solved the error in two steps, first I made the filter and then I sorted the data, then I passed the filtered and sorted data to the datagrid, But obviously it’s simple to do what you say, so I made the change as you indicated.

Thanks @stefano.menci

partidas = app_tables.diario_partidas.search(
tables.order_by(“Fecha”, ascending=True),
tables.order_by(“Numero_Partida”, ascending=True),
Fecha=q.between(fecha_inicio, fecha_fin_incluyente),
)