'Table' object has no attribute 'id'

107 views
Skip to first unread message

Álvaro Campayo

unread,
Nov 7, 2024, 10:15:39 AM11/7/24
to py4web
Good afternoon,
I am trying to implement a Grid with a Postgresql BB.DD table, which by requirement, the table is implemented with a non-sequential key field, that is, it is not an id field.

When deleting a record using the Delete button, an error of the type "'Table' object has no attribute 'id'" is given.

In the declaration of the associated Grid, I define the "field_id" attribute with the key field of the table, since I understand that this way the table does not have an id field.

I attach details of the app:

--Model
dbProvincias.define_table('provincia',
Field('nprovincia', type='integer', requires=IS_NOT_EMPTY(), required=True,
label = 'Cód. Provincia'),
Field('sdescrip', type='string', length=100, requires=IS_NOT_EMPTY(),
label = 'Provincia'),
migrate=False,
primarykey=['nprovincia'],
singular='Provincia',
plural='Provincias'
)

--Controller
def provinciaControlador(path=None):
gridProvincia = Grid(path,
dbProvincias.provincia,
formstyle=FormStyleBulma,
grid_class_style=GridClassStyleBulma,
headings=['Cód. Provincia', 'Provincia'],
orderby=[dbProvincias.provincia.nprovincia],
search_queries=[
[T('search_cod_provincia'), lambda value: dbProvincias.provincia.nprovincia == int(value)],
[T('search_provincia'), lambda value: dbProvincias.provincia.sdescrip.contains(value)]
],
field_id=dbProvincias.provincia.nprovincia,
deletable= on_deleteable_provincia_validation
)

Traceback:
Traceback (most recent call last):
  File "D:\acampayo\Entorno de Trabajo\Python\py4web_pruebas\py4web\core.py", line 1040, in wrapper
    ret = func(*func_args, **func_kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\acampayo\Entorno de Trabajo\Python\py4web_pruebas\py4web\core.py", line 1025, in wrapper
    raise exception
  File "D:\acampayo\Entorno de Trabajo\Python\py4web_pruebas\py4web\core.py", line 1001, in wrapper
    context["output"] = func(*args, **kwargs)
                        ^^^^^^^^^^^^^^^^^^^^^
  File "D:\acampayo\Entorno de Trabajo\Python\py4web_pruebas\apps\Saneamiento_CLM_Pru\controladores\configuracion\provinciaControlador.py", line 20, in provinciaControlador
    gridProvincia = Grid(path,
                    ^^^^^^^^^^
  File "D:\acampayo\Entorno de Trabajo\Python\py4web_pruebas\py4web\utils\grid.py", line 426, in __init__
    self.process()
  File "D:\acampayo\Entorno de Trabajo\Python\py4web_pruebas\py4web\utils\grid.py", line 642, in process
    db(db[self.tablename].id == self.record_id).delete()
       ^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'Table' object has no attribute 'id'

Thank you so much

Massimo

unread,
Nov 8, 2024, 1:59:57 AM11/8/24
to py4web
Looks like your table does not have a field "id". You must be using a different model than the one you show. In any case, I made grid.py more resilient to tables with a primary id not called "id" but I cannot promise that it will work. In particular forms and forms in grids are not designed to work without an "id" field.

Álvaro Campayo

unread,
Nov 8, 2024, 3:21:00 AM11/8/24
to py4web
Good morning,
Thank you very much for the instructions.
Reply all
Reply to author
Forward
0 new messages