我的表類看起來很典型,除了它可能包含一個before_render()函數。before_render的偉大之處在于我可以接觸到自己。這使我可以訪問有關我正在使用的模型的動態信息。如何訪問動態信息(如來自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'
1 回答

四季花海
TA貢獻1811條經驗 獲得超5個贊
所以這似乎有點奇怪,但它有效
class DynamicTable(tables.Table):
...
def before_render(self, request):
self.data.data = self.data.data.order_by('id')
self.paginate()
...
添加回答
舉報
0/150
提交
取消