私のDjangoプロジェクトでは、カラムが動的でデータベースの内容に依存するテーブルが必要です。だから私はhereの解決策を見つけた、それは少し問題があるが動作します。ここで私は、動的に拡張していますテーブルを持つクラスです:Django-tables2 - テーブルにカラムを動的に追加する - htmlのテーブルタグにattrsを追加しない
def define_table(roles):
attrs = dict((r.name, tables.Column() for r in roles)
klass = type('DynamicTable', (ClientsTable,), attrs)
return klass
私はこのようなviews.pyでテーブルを作成しています:
class ClientsTable(tables.Table):
class Meta:
model = Client
attrs = {"class": "paleblue", "orderable":"True", "width":"100%"}
fields = ('name',)
def __init__(self, *args, **kwargs):
super(ClientsTable, self).__init__(*args, **kwargs)
self.counter = itertools.count()
def render_row_number(self):
return '%d' % next(self.counter)
def render_id(self, value):
return '%s' % value
そしてここでは、クラスを拡張する方法は次のとおりです。私が望んでいたよう
table = define_table(roles)(queryset)
表には、列を示しているが、htmlコードで私はそれがattrsに無視することを参照してください。
{"class": "paleblue", "orderable":"True", "width":"100%"}
私にとって重要なpaleblueのCSSスタイルはありません。 私はそれがMetaクラスではあるかもしれないと感じますが、フィールドとモデルは機能しているので、attrsがなぜそうでないのかわかりません。
ニースのもの!私の問題には厳密には言及していませんが、問題を解決するためにスニペットを修正し、Pythonの 'type'とメタクラスの動作について多くのことを学びました。 Thx a束! – schwobaseggl
私は助けてくれてうれしい!私はまた、私のブログでさまざまな投稿を書いています。http://spapas.github.io/category/django.html – Serafeim