Creating a Table Class
A table class defines how your model's data is displayed in the CRUD list view — columns, sorting, search, and row actions.
Imports
from ...packages.crud.table.base import ModelTable
from ...packages.crud.table.column import ModelCol, StringCol
from .models import Patient
:::info Relative Import Depth The number of dots depends on your module's depth from the app root.
| Module location | Dots needed |
|---|---|
backend/patients/tables.py | from ...packages (3 dots) |
backend/masters/geography/tables.py | from ....packages (4 dots) |
| ::: |
Basic Table Class
class PatientTable(ModelTable):
name = ModelCol(display_as="Name", sortable=True, searchable=True)
contact = ModelCol(display_as="Contact", sortable=False, searchable=True)
address = ModelCol(display_as="Address", sortable=False, searchable=False)
class Meta:
model = Patient
The Meta.model tells the table which model's records to list. Each ModelCol maps to a field on that model.
Column Types
| Class | Use case |
|---|---|
ModelCol | A field that exists on the model |
StringCol | A computed/derived column — value comes from a _getval method |
StringCol (Computed Column)
Use StringCol when the column value is not a direct model field:
class PatientTable(ModelTable):
status_display = StringCol(display_as="Status", sortable=False, searchable=False)
class Meta:
model = Patient
def status_display_getval(self, obj):
return "Active" if obj.is_active else "Inactive"
The method name must follow the pattern <column_name>_getval(self, obj).