jieter / django-tables2

django-tables2 - An app for creating HTML tables
https://django-tables2.readthedocs.io/en/latest/
Other
1.9k stars 429 forks source link

Show related columns problem #256

Closed thebarty closed 8 years ago

thebarty commented 9 years ago

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:

# models.py
class Contact(models.Model):
    # ...

class Phone(models.Model):
    """
    https://github.com/stefanfoulis/django-phonenumber-field
    """
    #1 Contact has N Phone-Numbers
    contact = models.ForeignKey(
        Contact,
        related_name='phone', related_query_name='phone',
        verbose_name='Thhe Number',
    )
    nr = PhoneNumberField(
        blank=False,
        verbose_name='Number',
    )

# tables.py
class ContactTable(tables.Table):
    phone = tables.Column(accessor='phone')

    class Meta:
        model = Contact

# view.py
class Table2ListView(SingleTableView):
    model = Contact
    table_class = ContactTable
hdmaker commented 9 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.

thebarty commented 9 years ago

@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:

thebarty commented 9 years ago

I think I'll leave this issue open, as the accessor version in my original code is supposed to be working?

hdmaker commented 9 years ago

@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.

jieter commented 8 years ago

211 has a solution, I'll look into that later.