Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show related columns problem #256

Closed
thebarty opened this issue Jul 23, 2015 · 5 comments
Closed

Show related columns problem #256

thebarty opened this issue Jul 23, 2015 · 5 comments

Comments

@thebarty
Copy link

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
Copy link

hdmaker commented Jul 23, 2015

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
Copy link
Author

@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
Copy link
Author

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

@hdmaker
Copy link

hdmaker commented Jul 24, 2015

@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
Copy link
Owner

jieter commented Dec 16, 2015

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

@jieter jieter closed this as completed Dec 16, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants