Lia*_*nen 5 python django django-tables2
我的表类看起来很典型,只是它可能包含一个 before_render() 函数。before_render 的优点是我可以访问 self. 这使我可以访问有关我正在使用的模型的动态信息。如何访问动态信息(例如来自 before_render)以更改 Meta 类中的 order_by 变量?
def control_columns(table_self):
# Changes yesno for all Boolean fields to ('Yes','No') instead of the default check-mark or 'X'.
for column in table_self.data.table.columns.columns:
current_column = table_self.data.table.columns.columns[column].column
if isinstance(current_column,tables.columns.booleancolumn.BooleanColumn):
current_column.yesno = ('Yes','No')
class DynamicTable(tables.Table):
def before_render(self, request):
control_columns(self)
class Meta:
template_name = 'django_tables2/bootstrap4.html'
attrs = {'class': 'custom_table', 'tr': {'valign':"top"}}
order_by = 'index'
Run Code Online (Sandbox Code Playgroud)
所以这看起来有点奇怪,但它确实有效
class DynamicTable(tables.Table):
...
def before_render(self, request):
self.data.data = self.data.data.order_by('id')
self.paginate()
...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
163 次 |
| 最近记录: |