python - django-table2: Table subclassing and Meta inner class attributes -


i'm trying create generic table view, take model , tuple of columns names, , show given columns of model. problem inner meta class.

assume 2 models:

# models.py  django.db import models   class model_a(models.model):     column_a_1 = models.field()      column_a_2 = models.field()     column_a_3 = models.field()     column_a_4 = models.field()     column_a_5 = models.field()   class model_b(models.model):     column_b_1 = models.field()      column_b_2 = models.field()     column_b_3 = models.field()     column_b_4 = models.field()     column_b_5 = models.field() 

the objective have single view generates right table of columns in given order. so, i've urls.py this:

# urls.py  django.conf.urls import patterns, url import myapp.views myviews import myapp.models mymodels   urlpatterns = patterns( '',                        url( r'^table_a/$',                             myviews.show_table,                             { 'model': mymodels.model_a,                               'columns': ("column_a_1", "column_a_2", "column_a_3") } ),                        url( r'^table_b/$',                             myviews.show_table,                             { 'model': mymodels.model_b,                               'columns': ("column_b_5", "column_b_2") } ) ) 

and simple views.py this:

# views.py  django.shortcuts import render django_tables2 import requestconfig import myapp.tables mytables   def show_table(request, model, columns):     entries = model.objects.all()     table = mytables.modeltable(entries, model, columns)     requestconfig(request).configure(table)     return render(request, 'table_view.html', {'dynamic-table': table}) 

the key in table subclassing, inside tables.py:

# tables.py  django_tables2 import import table   class modeltable(table):      class meta:         pass      def __init__(self, data, model, columns, order_by=none, orderable=none, empty_text=none,                  exclude=none, attrs=none, sequence=none, prefix=none,                  order_by_field=none, page_field=none, per_page_field=none,                  template=none, sortable=none, default=none):         self.meta.model = model         self.meta.fields = columns         self.meta.sequence = columns         super(modeltable, self).__init__(data, order_by, orderable, empty_text,                  exclude, attrs, sequence, prefix,                  order_by_field, page_field, per_page_field,                  template, sortable, default) 

clearly, didn't understand how set attributes, of table.meta, table subclass initializer. courious thing when use {% render_table dynamic-table %} in template, en empty table (so not visible), working paginator.

where mistake?

solved. problem meta class: "lives" before modeltable initializer, setting meta attributes in said initializer pointless.

the revelation cames reading this qa.

i've define new method dinamically define specialized class; here new tables.py :

# tables.py  django_tables2 import import table   class modeltable(table):     class meta:         pass   def table_maker(model, columns):     meta_class = type('meta', (modeltable.meta,), {'model':model, 'fields':columns})     table_class = type(model._meta.object_name + 'table', (modeltable,), {'meta':meta_class})     return table_class 

and consequently views.py changes little bit:

# views.py  django.shortcuts import render django_tables2 import requestconfig import myapp.tables mytables   def show_table(request, model, columns):     entries = model.objects.all()     table = mytables.table_maker(model, columns)(entries)     requestconfig(request).configure(table)     return render(request, 'table_view.html', {'dynamic-table': table}) 

now works expected!


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -