I cannot find a replacement for the LinkColumn
in the new versions of django-tables2
. Author states that LinkColumn
is deprecated and shouldn't be used. But the new linkify
solution is poorly documented and doesn't have all the features of the old version. For example, I have this column:
edit = tables.LinkColumn(
'wagtailadmin_pages:edit', args=[A('page.pk')],
text='Edit'
)
It displays a link to the wagtail admin edit page called Edit
. There's simply no way to achieve the same using linkify
because linkify
only works if you have valid accessor
on the column. But accessor cannot return same static text for all rows (unless I modify the model to add a dummy property - but this particular model is in the 3rd party package and it would feel like a duct tape solution anyway).
In all other cases, column will not display a link. I've studied the source code and it seems that such case is simply not supported by the django-tables2 > 2.0.0.
Is there any clean and understandable way to construct a link column with a static link text using linkify
?
Answering my own question. It seems that it is impossible to fully replace LinkColumn
with the linkify
feature. The following code solves my problem:
from django.urls import reverse
from django.utils.text import mark_safe
import django_tables2 as tables
from wagtail.core.models import PageRevision
class WagtailRevisionsTable(tables.Table):
title = tables.Column(
accessor='page.title',
linkify=lambda record: record.page.url,
verbose_name='Title'
)
edit = tables.Column(
accessor='page.pk'
)
class Meta:
model = PageRevision
fields = ('title', 'created_at', 'user', 'edit')
template_name = 'django_tables2/bootstrap-responsive.html'
def render_edit(self, value):
url = reverse('wagtailadmin_pages:edit', args=[value])
return mark_safe(f'<a href="{url}">Edit</a>')
The code with the old LinkColumn
was much more concise, I don't understand the reason for change and documentations really doesn't help. There's simply not enough information on linkify
or render_col
methods.
So I hope this answer will help some poor soul trying to port old code to the django-tables2 >= 2.0.