I'm using django-tables2 for a project. I want to create a new column which links to the admin page of that model so it can be edited. Can I do that?
Asked
Active
Viewed 1,187 times
0
-
Does this question help? http://stackoverflow.com/questions/22941424/django-tables2-create-extra-column-with-links – Ben Apr 15 '17 at 21:14
1 Answers
2
Yes, you can. The admin views have a fix naming scheme. The change view url of a specific instance can be reversed from 'admin:appname_modelname_change'
and takes the instance's primary key as an argument:
from django_tables2.utils import A
column_name = tables.LinkColumn(
viewname = 'admin:applabel_modelname_change',
args=[A('pk')],
accessor=A('__str__') # or whatever attribute of your instance you want to display
)

user2390182
- 72,016
- 6
- 67
- 89
-
-
1`A` is a utility shortcut for the `Accessor` class from `django_tables2.utils`. Just import it. – user2390182 Apr 15 '17 at 21:23
-
-
Well, you can use the `accessor` param to set whatever field/property/method of your model you wnat to display. Just as with any other column. – user2390182 Apr 15 '17 at 21:39
-
Now I'm getting an error with the lookup `Reverse for 'view_booking_Booking_change' with arguments '(44,)' and keyword arguments '{}' not found. 0 pattern(s) tried: []` – user3080600 Apr 15 '17 at 21:44
-
What is the name of your app and your model? Both should be lowercased in the view name! – user2390182 Apr 15 '17 at 21:45
-
Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/141797/discussion-between-schwobaseggl-and-matt). – user2390182 Apr 15 '17 at 21:48