Closed thebarty closed 8 years ago
Hi MikeSmith12222,
You could use a render_FOO method to do something like that, to for example render the primary keys of the Phone
objects:
# tables.py
class ContactTable(tables.Table):
phone = tables.Column(empty_values=(),
verbose_name='Phone')
def render_phone(self, record):
# phone is the name of the related manager
if record.phone.exists():
return str([p.pk for p in record.phone.all()])
class Meta:
model = Contact
Nonetheless, I'd advise against giving phone
as a related_name and would prefer to use phones
since a Contact
can have many Phone
objects.
@hdmaker: Thank you so much for your quick response. This one really works.... Before I had tried to call a render function via an accessor which did not work. Your code snippet does the trick!!!
Thanks a lot and for all you other django newbie geeks - this is the reference in the tutorial http://django-tables2.readthedocs.org/en/latest/pages/custom-rendering.html.
P.S. by the way: i renamed the related_name to "phone_set". :+1:
I think I'll leave this issue open, as the accessor version in my original code is supposed to be working?
@MikeSmith12222 Glad I could help you.
Note that by default the related manager would have been called phone_set
if you hadn't given it a related_name
.
You can let that issue open or not as your convenience, that bug is already referenced in #211 and #229.
Hi guys,
damn.. I have spent around 4 hours on this problem.
I am using Django 1.8 with the latest django-tables2 and want to show some related data via the accessor option.
The problem: The rendered table does output the verbose table-header for "phone", BUT renders empty data for the column ("--").
Can you please give me a hint? I really need to get this to work asap.
Thanks a lot for your help!
This is my code that does NOT work: