0

I have a table:

db.define_table('mytable', Field('name', 'string'), Field('is_active', 'boolean', writable=False, readable=False, default=True), format='%(name)s')

for which I have enabled record versioning. Later I create a grid for mytable_archive. This grid has a current_record column which shows me the name of the record thanks to the format I specified early, but if I delete this record (web2py sets the is_active field to false) my grid don't shows me the format for my current_record in the format I specified early, it only shows the id. I know that the record isn't actually deleted, then why my grid shows me only this record id and not the name?

Sorry for my english and thanks in advance for any reponse.

Lben
  • 83
  • 1
  • 12

1 Answers1

1

When you specify the format argument, any reference field that references the table will get a default represent attribute that will fetch the referenced record from the table and apply the represent function to it. However, when using record versioning, deleted records (i.e., those with is_active == False) are automatically filtered out of all queries, so the represent function will fail to retrieve the referenced record and therefore be unable to generate the represented value (and will instead fall back to displaying the raw ID stored in the reference field).

If you think about it, though, once a record has been deleted from the original table, the idea of a current record doesn't really make sense. So, it would probably be more appropriate for the represent function of the current_record field to display something like None once the original record has been marked inactive.

Anyway, if you really want to see the name from the deleted record, before creating the grid that displays the archive table, you could temporarily disable the is_active filtering by doing:

db.mytable._common_filter = None

Note, if you do any subsequent queries on db.mytable in the same request, you would have to restore db.mytable._common_filter before proceeding:

common_filter, db.mytable._common_filter = db.mytable._common_filter, None
grid = SQLFORM.grid(db.mytable_archive, ...)
db.mytable._common_filter = common_filter
Anthony
  • 25,466
  • 3
  • 28
  • 57
  • db.mytable._common_filter = None disables the common filter only when I am requesting my archive grid? or it is disabled for the entire application? – Lben Dec 02 '15 at 22:08
  • All the code is executed on every request, so it will be disabled for the remainder of that request. On separate requests that don't execute that particular code, the filtering will still work. To be safe, you can always restore the `_common_filter` after creating the grid (I'll edit the answer with that option). – Anthony Dec 03 '15 at 02:40
  • Thank you so much, It works as expected. Now I can see what user deleted the record. – Lben Dec 03 '15 at 21:21