Customising the Link attribute in Horizon dashboard's DataTable

I had a quick dab at Django & Openstack Horizon frameworks in order to build a dashboard for one of our Openstack projects.

Our requirement was to show a table on the UI with the corresponding CRUD functions. I had used DataTable for this purpose, where in you define the table structure in tables.py and the view rendering is defined in views.py

If a column is defined to be a link, DataTable by default uses the object id of each row to generate the corresponding link. In my scenario, I needed the link to point to a different object. Openstack's documentation was not very helpful and a quick grep through the openstack's code gave me the idea.

The approach to generate a custom link is as follows inside a DataTable:

from django.core.urlresolvers import reverse

def get_custom_link(datum):
    return reverse('horizon:myproject:mydashboard:detail', 
                    kwargs={'key': datum.value})

The value of the key will be used to generate the URL. 

class MyDataTable(tables.DataTable):
   myvar = tables.Column('myvar_name', 
                          verbose_name=_("My Variable"), 
                         link=get_custom_link)

Comments

Popular posts from this blog

Solved: Fix for Git clone failure due to GnuTLS recv error (-9)

Openstack : Fixing Failed to create network. No tenant network is available for allocation issue.

QuickBite: Tap Vs Veth